Last active
March 21, 2016 10:35
-
-
Save i5on9i/9493bd1bd1923d87372c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hbwhale.seedmoney.tabs; | |
import android.Manifest; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.widget.Toast; | |
import com.hbwhale.seedmoney.R; | |
import com.hbwhale.seedmoney.utils.AndroidUtils; | |
import com.yalantis.ucrop.UCrop; | |
import java.io.File; | |
/** | |
* source from : https://github.com/Yalantis/uCrop/blob/master/sample/src/main/java/com/yalantis/ucrop/sample/BaseActivity.java | |
* modified by namh | |
*/ | |
public class BaseActivity extends AppCompatActivity { | |
protected static final int REQUEST_STORAGE_READ_ACCESS_PERMISSION = 101; | |
protected static final int REQUEST_STORAGE_WRITE_ACCESS_PERMISSION = 102; | |
private AlertDialog mAlertDialog; | |
/** | |
* Hide alert dialog if any. | |
*/ | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
if (mAlertDialog != null && mAlertDialog.isShowing()) { | |
mAlertDialog.dismiss(); | |
} | |
} | |
/** | |
* Requests given permission. | |
* If the permission has been denied previously, a Dialog will prompt the user to grant the | |
* permission, otherwise it is requested directly. | |
*/ | |
protected void requestPermission(final String permission, String rationale, final int requestCode) { | |
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) { | |
showAlertDialog(getString(R.string.permission_title_rationale), rationale, | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
ActivityCompat.requestPermissions(BaseActivity.this, | |
new String[]{permission}, requestCode); | |
} | |
}, getString(R.string.label_ok), null, getString(R.string.label_cancel)); | |
} else { | |
ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode); | |
} | |
} | |
/** | |
* This method shows dialog with given title & message. | |
* Also there is an option to pass onClickListener for positive & negative button. | |
* | |
* @param title - dialog title | |
* @param message - dialog message | |
* @param onPositiveButtonClickListener - listener for positive button | |
* @param positiveText - positive button text | |
* @param onNegativeButtonClickListener - listener for negative button | |
* @param negativeText - negative button text | |
*/ | |
protected void showAlertDialog(@Nullable String title, @Nullable String message, | |
@Nullable DialogInterface.OnClickListener onPositiveButtonClickListener, | |
@NonNull String positiveText, | |
@Nullable DialogInterface.OnClickListener onNegativeButtonClickListener, | |
@NonNull String negativeText) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
builder.setTitle(title); | |
builder.setMessage(message); | |
builder.setPositiveButton(positiveText, onPositiveButtonClickListener); | |
builder.setNegativeButton(negativeText, onNegativeButtonClickListener); | |
mAlertDialog = builder.show(); | |
} | |
//----------------------------------------------------> for cropper | |
protected final int REQUEST_SELECT_PICTURE = 0x01; | |
private static final String SAMPLE_CROPPED_IMAGE_NAME = "SampleCropImage.jpeg"; | |
protected Uri mDestinationUri; | |
private boolean needRequestPermission(Context context){ | |
return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN // Permission was added in API Level 16 | |
&& | |
ActivityCompat.checkSelfPermission( | |
context, | |
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED); | |
} | |
protected void pickFromGallery() { | |
if(needRequestPermission(this)){ | |
requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE, | |
getString(R.string.permission_read_storage_rationale), | |
REQUEST_STORAGE_READ_ACCESS_PERMISSION); | |
} else { | |
Intent intent = new Intent(); | |
intent.setType("image/*"); | |
intent.setAction(Intent.ACTION_GET_CONTENT); | |
intent.addCategory(Intent.CATEGORY_OPENABLE); | |
startActivityForResult( | |
Intent.createChooser(intent, getString(R.string.label_select_picture)), | |
REQUEST_SELECT_PICTURE); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (resultCode == RESULT_OK) { | |
//////////////////// | |
// <-- from gallery | |
if (requestCode == REQUEST_SELECT_PICTURE) { | |
final Uri selectedUri = data.getData(); | |
if (selectedUri != null) { | |
startCropActivity(data.getData()); | |
} else { | |
Toast.makeText(this, R.string.toast_cannot_retrieve_selected_image, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
//////////////////// | |
// <-- from cropper | |
else if (requestCode == UCrop.REQUEST_CROP) { | |
handleCropResult(data); | |
} | |
} | |
if (resultCode == UCrop.RESULT_ERROR) { | |
handleCropError(data); | |
} | |
} | |
private void startCropActivity(@NonNull Uri uri) { | |
if(mDestinationUri == null){ | |
mDestinationUri = Uri.fromFile(new File(getCacheDir(), SAMPLE_CROPPED_IMAGE_NAME)); | |
} | |
UCrop.of(uri, mDestinationUri).start(getActivity()); | |
} | |
protected Activity getActivity(){ | |
return this; | |
} | |
protected void handleCropResult(@NonNull Intent result) { | |
// final Uri resultUri = UCrop.getOutput(result); | |
// if (resultUri != null) { | |
// ResultActivity.startWithUri(SampleActivity.this, resultUri); | |
// } else { | |
// Toast.makeText(SampleActivity.this, R.string.toast_cannot_retrieve_cropped_image, Toast.LENGTH_SHORT).show(); | |
// } | |
} | |
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") | |
protected void handleCropError(@NonNull Intent result) { | |
// final Throwable cropError = UCrop.getError(result); | |
// if (cropError != null) { | |
// Log.e(TAG, "handleCropError: ", cropError); | |
// Toast.makeText(SampleActivity.this, cropError.getMessage(), Toast.LENGTH_LONG).show(); | |
// } else { | |
// Toast.makeText(SampleActivity.this, R.string.toast_unexpected_error, Toast.LENGTH_SHORT).show(); | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment