diff --git a/app/src/basic/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java b/app/src/basic/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java
index d53d0b937..1829a4439 100644
--- a/app/src/basic/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java
+++ b/app/src/basic/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java
@@ -1,9 +1,9 @@
package org.fdroid.fdroid.views.main;
-import android.app.Activity;
+import android.content.Context;
class NearbyViewBinder {
- public static void updateUsbOtg(final Activity activity) {
+ public static void updateUsbOtg(Context context) {
throw new IllegalStateException("unimplemented");
}
}
diff --git a/app/src/full/AndroidManifest.xml b/app/src/full/AndroidManifest.xml
index 41fb74cfe..abfb948e4 100644
--- a/app/src/full/AndroidManifest.xml
+++ b/app/src/full/AndroidManifest.xml
@@ -91,16 +91,25 @@
android:name=".nearby.SDCardScannerService"
android:exported="false"/>
-
+
-
+
+
+
+
+
+
+
+
= 19) {
- ContentResolver contentResolver = activity.getContentResolver();
+ ContentResolver contentResolver = context.getContentResolver();
int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
contentResolver.takePersistableUriPermission(uri, perms);
}
- String msg = String.format(activity.getString(R.string.swap_toast_using_path), uri.toString());
- Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
- scan(activity, uri);
+ String msg = String.format(context.getString(R.string.swap_toast_using_path), uri.toString());
+ Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
+ scan(context, uri);
}
}
diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/UsbDeviceAttachedActivity.java b/app/src/full/java/org/fdroid/fdroid/nearby/UsbDeviceAttachedReceiver.java
similarity index 62%
rename from app/src/full/java/org/fdroid/fdroid/nearby/UsbDeviceAttachedActivity.java
rename to app/src/full/java/org/fdroid/fdroid/nearby/UsbDeviceAttachedReceiver.java
index 3834d0aea..d5fdfb90f 100644
--- a/app/src/full/java/org/fdroid/fdroid/nearby/UsbDeviceAttachedActivity.java
+++ b/app/src/full/java/org/fdroid/fdroid/nearby/UsbDeviceAttachedReceiver.java
@@ -44,47 +44,28 @@ import java.util.HashMap;
/**
* This is just a shim to receive {@link UsbManager#ACTION_USB_ACCESSORY_ATTACHED}
- * events then open up the right screen in {@link MainActivity}.
+ * events.
*/
-public class UsbDeviceAttachedActivity extends Activity {
- public static final String TAG = "UsbDeviceAttachedActivi";
+public class UsbDeviceAttachedReceiver extends BroadcastReceiver {
+ public static final String TAG = "UsbDeviceAttachedReceiv";
private static final HashMap contentObservers = new HashMap<>();
@RequiresApi(api = 19)
@Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
+ public void onReceive(final Context context, Intent intent) {
if (Build.VERSION.SDK_INT < 19) {
- finish();
return;
}
- Intent intent = getIntent();
if (intent == null || TextUtils.isEmpty(intent.getAction())
|| !UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
Log.i(TAG, "ignoring irrelevant intent: " + intent);
- finish();
return;
}
Log.i(TAG, "handling intent: " + intent);
- final ContentResolver contentResolver = getContentResolver();
- BroadcastReceiver receiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- if (!UsbManager.ACTION_USB_DEVICE_DETACHED.equals(intent.getAction())) {
- return;
- }
- NearbyViewBinder.updateUsbOtg(UsbDeviceAttachedActivity.this);
- unregisterReceiver(this);
- for (ContentObserver contentObserver : contentObservers.values()) {
- contentResolver.unregisterContentObserver(contentObserver);
- }
- }
- };
- registerReceiver(receiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
+ final ContentResolver contentResolver = context.getContentResolver();
for (final UriPermission uriPermission : contentResolver.getPersistedUriPermissions()) {
Uri uri = uriPermission.getUri();
@@ -92,21 +73,10 @@ public class UsbDeviceAttachedActivity extends Activity {
@Override
public void onChange(boolean selfChange, Uri uri) {
- NearbyViewBinder.updateUsbOtg(UsbDeviceAttachedActivity.this);
+ NearbyViewBinder.updateUsbOtg(context);
}
};
contentResolver.registerContentObserver(uri, true, contentObserver);
}
- intent.setComponent(new ComponentName(this, MainActivity.class));
- intent.putExtra(MainActivity.EXTRA_VIEW_NEARBY, true);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- startActivity(intent);
- finish();
- }
-
- @Override
- public void finish() {
- setResult(RESULT_OK);
- super.finish();
}
}
diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/UsbDeviceDetachedReceiver.java b/app/src/full/java/org/fdroid/fdroid/nearby/UsbDeviceDetachedReceiver.java
new file mode 100644
index 000000000..540f813cc
--- /dev/null
+++ b/app/src/full/java/org/fdroid/fdroid/nearby/UsbDeviceDetachedReceiver.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2018-2019 Hans-Christoph Steiner
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+
+package org.fdroid.fdroid.nearby;
+
+import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.UriPermission;
+import android.database.ContentObserver;
+import android.hardware.usb.UsbManager;
+import android.net.Uri;
+import android.os.Build;
+import android.os.Bundle;
+import android.os.Handler;
+import android.support.annotation.Nullable;
+import android.support.annotation.RequiresApi;
+import android.text.TextUtils;
+import android.util.Log;
+import org.fdroid.fdroid.views.main.MainActivity;
+import org.fdroid.fdroid.views.main.NearbyViewBinder;
+
+import java.util.HashMap;
+
+/**
+ * This is just a shim to receive {@link UsbManager#ACTION_USB_DEVICE_DETACHED}
+ * events.
+ */
+public class UsbDeviceDetachedReceiver extends BroadcastReceiver {
+ public static final String TAG = "UsbDeviceDetachedReceiv";
+
+ private static final HashMap contentObservers = new HashMap<>();
+
+ @RequiresApi(api = 19)
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (Build.VERSION.SDK_INT < 19) {
+ return;
+ }
+
+ if (intent == null || TextUtils.isEmpty(intent.getAction())
+ || !UsbManager.ACTION_USB_DEVICE_DETACHED.equals(intent.getAction())) {
+ Log.i(TAG, "ignoring irrelevant intent: " + intent);
+ return;
+ }
+ Log.i(TAG, "handling intent: " + intent);
+
+ final ContentResolver contentResolver = context.getContentResolver();
+ NearbyViewBinder.updateUsbOtg(context);
+ for (ContentObserver contentObserver : contentObservers.values()) {
+ contentResolver.unregisterContentObserver(contentObserver);
+ }
+ }
+}
diff --git a/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java b/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java
index f6e20dac9..bf0a84349 100644
--- a/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java
+++ b/app/src/full/java/org/fdroid/fdroid/views/main/NearbyViewBinder.java
@@ -146,7 +146,7 @@ public class NearbyViewBinder {
updateUsbOtg(activity);
}
- public static void updateUsbOtg(final Activity activity) {
+ public static void updateUsbOtg(final Context context) {
if (Build.VERSION.SDK_INT < 24) {
return;
}
@@ -159,7 +159,7 @@ public class NearbyViewBinder {
storageVolumeText.setVisibility(View.GONE);
requestStorageVolume.setVisibility(View.GONE);
- final StorageManager storageManager = (StorageManager) activity.getSystemService(Context.STORAGE_SERVICE);
+ final StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
for (final StorageVolume storageVolume : storageManager.getStorageVolumes()) {
if (storageVolume.isRemovable() && !storageVolume.isPrimary()) {
Log.i(TAG, "StorageVolume: " + storageVolume);
@@ -170,13 +170,13 @@ public class NearbyViewBinder {
}
storageVolumeText.setVisibility(View.VISIBLE);
- String text = storageVolume.getDescription(activity);
+ String text = storageVolume.getDescription(context);
if (!TextUtils.isEmpty(text)) {
requestStorageVolume.setText(text);
UsbDevice usb = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (usb != null) {
text = String.format("%s (%s %s)", text, usb.getManufacturerName(), usb.getProductName());
- Toast.makeText(activity, text, Toast.LENGTH_LONG).show();
+ Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
}
@@ -185,16 +185,17 @@ public class NearbyViewBinder {
@Override
@RequiresApi(api = 24)
public void onClick(View v) {
- List list = activity.getContentResolver().getPersistedUriPermissions();
+ List list = context.getContentResolver().getPersistedUriPermissions();
if (list != null) for (UriPermission uriPermission : list) {
Uri uri = uriPermission.getUri();
if (uri.getPath().equals(String.format("/tree/%s:", storageVolume.getUuid()))) {
intent.setData(uri);
- TreeUriScannerIntentService.onActivityResult(activity, intent);
+ TreeUriScannerIntentService.onActivityResult(context, intent);
return;
}
}
- activity.startActivityForResult(intent, MainActivity.REQUEST_STORAGE_ACCESS);
+ ((Activity) context).startActivityForResult(intent,
+ MainActivity.REQUEST_STORAGE_ACCESS);
}
});
}