1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 09:17:29 +01:00

Simplify external dir handling again -- prefer the primary dir

The primary external dir is often a user-partition on the internal
storage medium. This one is safe in multi-user environments.

The "removable storage" (sdcard) on the other hand can be read by
everyone. If the former is not available or not writable, use the
latter.

closes #153
This commit is contained in:
cpfeiffer 2015-11-01 20:49:50 +01:00
parent 8920f5e95b
commit d4f070f0aa
4 changed files with 24 additions and 11 deletions

View File

@ -92,7 +92,7 @@ public class AppManagerActivity extends Activity {
} }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LOG.error("Error getting cached apps: " + e.getMessage(), e);
} }
return cachedAppList; return cachedAppList;
} }

View File

@ -3,6 +3,9 @@ package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -16,6 +19,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.GenericItem;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
public class PBWInstallHandler implements InstallHandler { public class PBWInstallHandler implements InstallHandler {
private static final Logger LOG = LoggerFactory.getLogger(PBWInstallHandler.class);
private final Context mContext; private final Context mContext;
private PBWReader mPBWReader; private PBWReader mPBWReader;
@ -110,7 +114,7 @@ public class PBWInstallHandler implements InstallHandler {
destDir.mkdirs(); destDir.mkdirs();
FileUtils.copyFile(pbwFile, new File(destDir + "/" + app.getUUID().toString() + ".pbw")); FileUtils.copyFile(pbwFile, new File(destDir + "/" + app.getUUID().toString() + ".pbw"));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LOG.error("Installation failed: " + e.getMessage(), e);
} }
} }

View File

@ -491,7 +491,7 @@ public class PebbleIoThread extends GBDeviceIoThread {
try { try {
installApp(Uri.fromFile(new File(FileUtils.getExternalFilesDir() + "/pbw-cache/" + appMgmt.uuid.toString() + ".pbw")), appMgmt.token); installApp(Uri.fromFile(new File(FileUtils.getExternalFilesDir() + "/pbw-cache/" + appMgmt.uuid.toString() + ".pbw")), appMgmt.token);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LOG.error("Error installing app: " + e.getMessage(), e);
} }
break; break;
default: default:

View File

@ -2,6 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge.util;
import android.content.Context; import android.content.Context;
import android.os.Environment; import android.os.Environment;
import android.support.annotation.NonNull;
import android.util.Log; import android.util.Log;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -42,7 +43,8 @@ public class FileUtils {
} }
/** /**
* Returns the existing external storage dir. * Returns the existing external storage dir. The directory is guaranteed to
* exist and to be writable.
* *
* @throws IOException when the directory is not available * @throws IOException when the directory is not available
*/ */
@ -72,6 +74,17 @@ public class FileUtils {
} }
} }
/**
* Returns a list of directories to write to. The list is sorted by priority,
* i.e. the first directory should be preferred, the last one is the least
* preferred one.
*
* Note that the directories may not exist, so it is not guaranteed that you
* can actually write to them. But when created, they *should* be writable.
* @return the list of writable directories
* @throws IOException
*/
@NonNull
private static List<File> getWritableExternalFilesDirs() throws IOException { private static List<File> getWritableExternalFilesDirs() throws IOException {
Context context = GBApplication.getContext(); Context context = GBApplication.getContext();
File[] dirs = context.getExternalFilesDirs(null); File[] dirs = context.getExternalFilesDirs(null);
@ -87,17 +100,13 @@ public class FileUtils {
if (!dir.exists() && !dir.mkdirs()) { if (!dir.exists() && !dir.mkdirs()) {
continue; continue;
} }
// if (!dir.canWrite() || !Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState(dir))) { // the first directory is also the primary external storage, i.e. the same as Environment.getExternalFilesDir()
// TODO: check the mount state of *all* dirs when switching to later API level
if (!dir.canWrite() || (i == 0 && !Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))) { if (!dir.canWrite() || (i == 0 && !Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))) {
Log.i(TAG, "ignoring non-writable external storage dir: " + dir); Log.i(TAG, "ignoring non-writable external storage dir: " + dir);
continue; continue;
} }
// if (Environment.isExternalStorageEmulated(dir)) { result.add(dir); // add last
if (i == 0 && Environment.isExternalStorageEmulated()) {
result.add(dir); // add last
} else {
result.add(0, dir); // add first
}
} }
return result; return result;
} }