From 530bf2961f75ef584587277fc11b1ed396090972 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Thu, 14 May 2015 00:18:45 +0200 Subject: [PATCH] Prevent some crashes --- .../sticktoalbum/MainActivity.java | 147 +++++++++++++++--- .../sticktoalbum/SettingsActivity.java | 4 +- app/src/main/res/values-de/strings.xml | 5 + app/src/main/res/values/strings.xml | 5 + .../res/values/strings_activity_settings.xml | 61 -------- 5 files changed, 135 insertions(+), 87 deletions(-) delete mode 100644 app/src/main/res/values/strings_activity_settings.xml diff --git a/app/src/main/java/de/vanitasvitae/sticktoalbum/MainActivity.java b/app/src/main/java/de/vanitasvitae/sticktoalbum/MainActivity.java index 0a35789..9aa1c28 100644 --- a/app/src/main/java/de/vanitasvitae/sticktoalbum/MainActivity.java +++ b/app/src/main/java/de/vanitasvitae/sticktoalbum/MainActivity.java @@ -42,7 +42,7 @@ import java.util.ArrayList; */ public class MainActivity extends ActionBarActivity { - private ArrayList sharedMedia; + private ArrayList sharedMedia; private static String Tag = "stickToAlbum"; SharedPreferences sharedPref = null; @@ -53,13 +53,11 @@ public class MainActivity extends ActionBarActivity setContentView(R.layout.activity_main); sharedPref = PreferenceManager.getDefaultSharedPreferences(this); - String appDirectoryPath = sharedPref.getString(SettingsActivity.KEY_PREF_APP_PATH, ""); - - if(appDirectoryPath.isEmpty()) + if(sharedPref.getString(SettingsActivity.KEY_PREF_APP_PATH, "").isEmpty()) { - appDirectoryPath = resetAppDirectoryPath(); + resetAppDirectoryPath(); } - sharedMedia = new ArrayList(); + sharedMedia = new ArrayList<>(); Intent intent = getIntent(); String action = intent.getAction(); @@ -84,6 +82,9 @@ public class MainActivity extends ActionBarActivity } } + /** + * Handle first run + */ protected void onResume() { super.onResume(); @@ -103,40 +104,58 @@ public class MainActivity extends ActionBarActivity }); firstRunDialog.setCancelable(false); firstRunDialog.show(); - sharedPref.edit().putBoolean("firstrun", false).commit(); + sharedPref.edit().putBoolean("firstrun", false).apply(); } } /** * Handle one single file being shared with the application. * Add the files Uri to the list of files to move and open the saveDialog. - * @param intent + * @param intent Intent */ void handleSendSingleFile(Intent intent) { Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); if (imageUri != null) { - sharedMedia.add(imageUri); + String url = getRealPathFromURI(this, imageUri); + if(url != null) + { + sharedMedia.add(url); + openSaveDialog(); + } + else + { + handleUnsupportedMedia(); + } } - openSaveDialog(); } /** * Handle multiple files being shared with the application. * Add the files Uris to the list of files to move and open the saveDialog. - * @param intent + * @param intent Intent */ void handleSendMultipleFiles(Intent intent) { ArrayList imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); - if (imageUris != null) { - sharedMedia = imageUris; + if (imageUris != null) + { + for(Uri u : imageUris) + { + String url = getRealPathFromURI(this, u); + if(url != null) + { + sharedMedia.add(url); + } + } } - openSaveDialog(); + if(imageUris == null || imageUris.size()==0) handleUnsupportedMedia(); + else openSaveDialog(); } /** * Show Dialog to inform the user about the fact, that the feature he/she wants to use is not * implemented */ + @SuppressWarnings("unused") void handleUnimplementedFeature() { AlertDialog.Builder builder = new AlertDialog.Builder(this); @@ -154,6 +173,43 @@ public class MainActivity extends ActionBarActivity builder.show(); } + void handleUnsupportedMedia() + { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.dialog_title_unsupported_media); + builder.setMessage(R.string.dialog_content_unsupported_media); + builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() + { + @Override + public void onClick(DialogInterface dialog, int which) + { + dialog.cancel(); + finish(); + } + }); + builder.show(); + } + + void handleCouldNotWrite(String... urls) + { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.dialog_title_write_failed); + String msg = getResources().getString(R.string.dialog_content_write_failed) + "\n\n"; + for(String u : urls) msg = msg + u + "\n"; + msg = msg.substring(0,msg.length()-1); + builder.setMessage(msg); + builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() + { + @Override + public void onClick(DialogInterface dialog, int which) + { + dialog.cancel(); + finish(); + } + }); + builder.show(); + } + /** * Open a Dialog that prompts the user to enter an album name. * Reopens dialog, if album name is empty. @@ -173,7 +229,7 @@ public class MainActivity extends ActionBarActivity input.setInputType(InputType.TYPE_CLASS_TEXT); ListView existingAlbums = (ListView) dialogView.findViewById(R.id.album_list); final ArrayList directoryList = getFoldersOnDirectory(getAppDirectoryPath()); - ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, directoryList); + ArrayAdapter adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, directoryList); existingAlbums.setAdapter(adapter); existingAlbums.setOnItemClickListener(new AdapterView.OnItemClickListener() { @@ -247,13 +303,12 @@ public class MainActivity extends ActionBarActivity /** * Move selected files to a new album. The album is located at appDirectoryPath/albumName - * @param albumName + * @param albumName name of the Album to move the files to */ private void moveFiles(String albumName) { - for(Uri u: sharedMedia) + for(String srcDest : sharedMedia) { - String srcDest = getRealPathFromURI(this, u); if(!moveImageFromTo(srcDest, calcTargetPath(albumName, srcDest))) { System.out.println("Fail: " +srcDest); @@ -263,9 +318,9 @@ public class MainActivity extends ActionBarActivity /** * Move file src to target. - * @param src - * @param target - * @return + * @param src url of the file that will be moved + * @param target url of the destination + * @return success */ private boolean moveImageFromTo(String src, String target) { @@ -316,6 +371,10 @@ public class MainActivity extends ActionBarActivity } } + /** + * Add the file on path to the MediaStore + * @param path url of the file + */ private void addFileToMediaStore(String path) { MediaScannerConnection.scanFile( @@ -332,6 +391,12 @@ public class MainActivity extends ActionBarActivity }); } + /** + * calculate the new filepath for a file according to the album name + * @param albumName name of the Album + * @param srcDest url of the file + * @return full url of the destination + */ private String calcTargetPath(String albumName, String srcDest) { String file = getAppDirectoryPath()+albumName+srcDest.substring(srcDest.lastIndexOf("/")); @@ -339,6 +404,10 @@ public class MainActivity extends ActionBarActivity return file; } + /** + * Set the apps directory path to /Pictures/Albums + * @return new path + */ public String resetAppDirectoryPath() { String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/Albums/"; @@ -349,6 +418,10 @@ public class MainActivity extends ActionBarActivity return path; } + /** + * return the current app directory path + * @return path where to store new albums + */ public String getAppDirectoryPath() { SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); @@ -360,6 +433,11 @@ public class MainActivity extends ActionBarActivity } } + /** + * Return a list of Strings that represent the names of the folders on dirPath + * @param dirPath path of the directory + * @return list of subfolders of the directory + */ public ArrayList getFoldersOnDirectory(String dirPath) { File parent = new File(dirPath); @@ -375,10 +453,17 @@ public class MainActivity extends ActionBarActivity return subDirs; } + /** + * Get a real path from a uri. + * @param context context + * @param contentUri uri + * @return real path + */ public String getRealPathFromURI(Context context, Uri contentUri) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null); + if(cursor == null) return null; int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String realPath = cursor.getString(column_index); @@ -387,6 +472,11 @@ public class MainActivity extends ActionBarActivity return realPath; } + /** + * Return the mimeType of the file on url + * @param url url + * @return mimeType (eg. "image/jpg") + */ public static String getMimeType(String url) { String type = null; @@ -406,19 +496,26 @@ public class MainActivity extends ActionBarActivity @Override protected Void doInBackground(String... params) { - InputStream in = null; - OutputStream out = null; + InputStream in; + OutputStream out; String src = params[0]; String target = params[1]; try { File dir = new File(src.substring(0, src.lastIndexOf("/"))); if (!dir.exists()) - dir.mkdirs(); + { + handleUnsupportedMedia(); + return null; + } in = new FileInputStream(src); File outFile = new File(target.substring(0, target.lastIndexOf("/"))); if (!outFile.exists()) - outFile.mkdirs(); + if(!outFile.mkdirs()) + { + handleCouldNotWrite(outFile.getAbsolutePath()); + return null; + } out = new FileOutputStream(target); byte[] buffer = new byte[1024]; diff --git a/app/src/main/java/de/vanitasvitae/sticktoalbum/SettingsActivity.java b/app/src/main/java/de/vanitasvitae/sticktoalbum/SettingsActivity.java index 0dc6db4..d88a54a 100644 --- a/app/src/main/java/de/vanitasvitae/sticktoalbum/SettingsActivity.java +++ b/app/src/main/java/de/vanitasvitae/sticktoalbum/SettingsActivity.java @@ -7,12 +7,14 @@ import android.preference.PreferenceActivity; import android.preference.PreferenceManager; /** - * Created by vanitas on 10.05.15. + * SettingsActivity for handling Preferences + * @author vanitas */ public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { public final static String KEY_PREF_APP_PATH = "pref_app_path"; + @SuppressWarnings("all") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 39659c2..22c0e5f 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -8,6 +8,10 @@ Diese Funktion ist noch nicht fertig implementiert.\nSchau mal nach Updates :) Achtung! Diese App ist in einer frühen Entwicklungsphase und könnte instabil sein! Sei vorsichtig, wenn du deine schönsten Bilder verwaltest. Die App könnte abstürzen und die geteilten Daten vernichten! + Nicht unterstützte Operation + Die geteilten Dateien werden nicht unterstützt.\nVerschiebe Fotos und Videos in neue Alben, indem du sie aus der Android Galerie App teilst. + Fehler! + Konnte folgende Dateien nicht schreiben. Vielleicht fehlen Berechtigungen? Bitte gebe einen Albumnamen ein! Einstellungen OK @@ -17,4 +21,5 @@ Speicherort Wo sollen neue Alben angelegt werden? Um den Speicherort auf den Standardwert zurückzusetzen, lösche diesen Wert und starte die App neu. + Einstellungen \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a84dc82..ea58998 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -8,6 +8,10 @@ This feature is not implemented yet.\nCheck out updates :) Warning! This app is an early alpha version and can be unstable! Do not rely on it while handling your most precious images. The app may crash and destroy your data! + Unsupported Media + The shared files are not supported.\nStick photos into albums by sharing them from Androids Gallery App. + Error! + Could not write the following files. Maybe missing Permissions? Please enter valid album name! Settings OK @@ -17,4 +21,5 @@ Album Directory In which directory are Albums located? Delete value and restart the app to reset this to default. + Settings \ No newline at end of file diff --git a/app/src/main/res/values/strings_activity_settings.xml b/app/src/main/res/values/strings_activity_settings.xml deleted file mode 100644 index 96455b2..0000000 --- a/app/src/main/res/values/strings_activity_settings.xml +++ /dev/null @@ -1,61 +0,0 @@ - - Settings - - - - - General - - Enable social recommendations - Recommendations for people to contact - based on your message history - - - Display name - John Smith - - Add friends to messages - - Always - When possible - Never - - - 1 - 0 - -1 - - - - Data & sync - - Sync frequency - - 15 minutes - 30 minutes - 1 hour - 3 hours - 6 hours - Never - - - 15 - 30 - 60 - 180 - 360 - -1 - - - System sync settings - - - Notifications - - New message notifications - - Ringtone - Silent - - Vibrate -