1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-06-16 08:34:55 +02:00

Added permission check to FileChooser and added missing file choosing method for Android 4.2 WIP WARNING

This commit is contained in:
vanitasvitae 2016-09-07 23:12:17 +02:00
parent 305a40c8c5
commit 7dbfb10229
3 changed files with 68 additions and 8 deletions

View file

@ -34,6 +34,7 @@ import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.Handler; import android.os.Handler;
@ -351,10 +352,59 @@ public class MainActivity extends AppCompatActivity
progressBar.setVisibility(progress == 100 ? View.GONE : View.VISIBLE); progressBar.setVisibility(progress == 100 ? View.GONE : View.VISIBLE);
} }
//For Android 4.1/4.2 only. DONT REMOVE
protected void openFileChooser(ValueCallback<Uri[]> uploadMsg, String acceptType, String capture)
{
Log.d(App.TAG, "openFileChooser(ValCallback<Uri[]>, String, String");
mFilePathCallback = uploadMsg;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),INPUT_FILE_REQUEST_CODE);
}
//For Android 4.1/4.2 only. DONT REMOVE
protected void openFileChooser(ValueCallback<Uri[]> uploadMsg)
{
Log.d(App.TAG, "openFileChooser(ValCallback<Uri[]>");
onShowFileChooser(webView, uploadMsg, null);
/*
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
*/
}
@Override @Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (mFilePathCallback != null) mFilePathCallback.onReceiveValue(null); if(Build.VERSION.SDK_INT >= 23) {
int hasWRITE_EXTERNAL_STORAGE = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWRITE_EXTERNAL_STORAGE != PackageManager.PERMISSION_GRANTED) {
if (!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(MainActivity.this)
.setMessage(R.string.permissions_image)
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (android.os.Build.VERSION.SDK_INT >= 23)
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
}
})
.show();
return false;
}
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
return false;
}
}
Log.d(App.TAG, "onOpenFileChooser");
if (mFilePathCallback != null) mFilePathCallback.onReceiveValue(null);
mFilePathCallback = filePathCallback; mFilePathCallback = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
@ -365,6 +415,7 @@ public class MainActivity extends AppCompatActivity
photoFile = Helpers.createImageFile(); photoFile = Helpers.createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) { } catch (IOException ex) {
Log.e(App.TAG, "ERROR creating temp file: "+ ex.toString());
// Error occurred while creating the File // Error occurred while creating the File
Snackbar.make(contentLayout, R.string.unable_to_load_image, Snackbar.LENGTH_LONG).show(); Snackbar.make(contentLayout, R.string.unable_to_load_image, Snackbar.LENGTH_LONG).show();
return false; return false;
@ -396,6 +447,7 @@ public class MainActivity extends AppCompatActivity
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
Log.d(App.TAG,"startActivityForResult");
startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE); startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);
return true; return true;
} }
@ -528,26 +580,32 @@ public class MainActivity extends AppCompatActivity
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(App.TAG,"onActivityResult:");
if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) { if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
Log.d(App.TAG,"reqCode != INPUT_FILE_REQUEST_CODE or mFilePathCallback == null");
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
return; return;
} }
Uri[] results = null; Uri[] results = null;
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
Log.d(App.TAG, "Activity.RESULT_OK");
if (data == null) { if (data == null) {
Log.d(App.TAG, "data == null");
if (mCameraPhotoPath != null) { if (mCameraPhotoPath != null) {
Log.d(App.TAG, "mCameraPhotoPath != null");
results = new Uri[]{Uri.parse(mCameraPhotoPath)}; results = new Uri[]{Uri.parse(mCameraPhotoPath)};
} }
} else { } else {
Log.d(App.TAG, "data != null");
String dataString = data.getDataString(); String dataString = data.getDataString();
if (dataString != null) { if (dataString != null) {
Log.d(App.TAG, "dataString != null");
results = new Uri[]{Uri.parse(dataString)}; results = new Uri[]{Uri.parse(dataString)};
} }
} }
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
} }
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
} }
@Override @Override

View file

@ -26,6 +26,7 @@ import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.util.Log; import android.util.Log;
import com.github.dfa.diaspora_android.App;
import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.R;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -62,12 +63,13 @@ public class Helpers {
// Create an image file name // Create an image file name
String timeStamp = new SimpleDateFormat("dd-MM-yy_HH-mm", Locale.getDefault()).format(new Date()); String timeStamp = new SimpleDateFormat("dd-MM-yy_HH-mm", Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_"; String imageFileName = "JPEG_" + timeStamp + "_";
Log.d(App.TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
File storageDir = Environment.getExternalStoragePublicDirectory( File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES); Environment.DIRECTORY_PICTURES);
return File.createTempFile( return new File (
imageFileName, /* prefix */ imageFileName + /* prefix */
".jpg", /* suffix */ ".jpg", /* suffix */
storageDir /* directory */ storageDir.getAbsolutePath() /* directory */
); );
} }

View file

@ -97,7 +97,7 @@
completely close the app or restart the device. If you don\'t permit the storage access but want to use the completely close the app or restart the device. If you don\'t permit the storage access but want to use the
screenshot function at a later time, you can grant the permission later. Please open then: systemsettings - apps - screenshot function at a later time, you can grant the permission later. Please open then: systemsettings - apps -
Diaspora. In the permissions section you can grant the \"write storage permission\".</string> Diaspora. In the permissions section you can grant the \"write storage permission\".</string>
<string name="permissions_image">You must grant \"Access Storage Permission\" to save images. After that you should <string name="permissions_image">You must grant \"Access Storage Permission\" to save/upload images. After that you should
completely close the app or restart the device. If you don\'t permit the storage access but want to save images completely close the app or restart the device. If you don\'t permit the storage access but want to save images
at a later time, you can grant the permission later. Please open then: systemsettings - apps - at a later time, you can grant the permission later. Please open then: systemsettings - apps -
Diaspora. In the permissions section you can grant the \"write storage permission\".</string> Diaspora. In the permissions section you can grant the \"write storage permission\".</string>