use MEDIA_* events to update USB-OTG detection
This means that sometimes the NearbyView is updated from a BroadcastReceiver's Context, which is not an Activity. So this has to try a little harder to fetch the Activity instance needed for the prompt to request permissions to a folder on the USB. This adds a failsafe to fallback to the file:/// scanning in SDCardScannerService.
This commit is contained in:
parent
6fd7970ca5
commit
6af66abf54
@ -100,7 +100,6 @@
|
||||
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
|
||||
android:resource="@xml/device_filter"/>
|
||||
</receiver>
|
||||
|
||||
<receiver
|
||||
android:name=".nearby.UsbDeviceDetachedReceiver">
|
||||
<intent-filter>
|
||||
@ -110,6 +109,17 @@
|
||||
android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
|
||||
android:resource="@xml/device_filter"/>
|
||||
</receiver>
|
||||
<receiver android:name=".nearby.UsbDeviceMediaMountedReceiver">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MEDIA_EJECT" />
|
||||
<action android:name="android.intent.action.MEDIA_REMOVED" />
|
||||
<action android:name="android.intent.action.MEDIA_MOUNTED" />
|
||||
<action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
|
||||
|
||||
<data android:scheme="content" />
|
||||
<data android:scheme="file" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<activity
|
||||
android:name=".panic.PanicPreferencesActivity"
|
||||
|
@ -0,0 +1,23 @@
|
||||
package org.fdroid.fdroid.nearby;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Environment;
|
||||
import org.fdroid.fdroid.views.main.NearbyViewBinder;
|
||||
|
||||
public class UsbDeviceMediaMountedReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent == null || intent.getAction() == null) {
|
||||
return;
|
||||
}
|
||||
String action = intent.getAction();
|
||||
if (Environment.MEDIA_BAD_REMOVAL.equals(action)
|
||||
|| Environment.MEDIA_MOUNTED.equals(action)
|
||||
|| Environment.MEDIA_REMOVED.equals(action)
|
||||
|| Environment.MEDIA_EJECTING.equals(action)) {
|
||||
NearbyViewBinder.updateUsbOtg(context);
|
||||
}
|
||||
}
|
||||
}
|
@ -196,8 +196,23 @@ public class NearbyViewBinder {
|
||||
return;
|
||||
}
|
||||
}
|
||||
((Activity) context).startActivityForResult(intent,
|
||||
MainActivity.REQUEST_STORAGE_ACCESS);
|
||||
|
||||
Activity activity = null;
|
||||
if (context instanceof Activity) {
|
||||
activity = (Activity) context;
|
||||
} else if (swapView != null && swapView.getContext() instanceof Activity) {
|
||||
activity = (Activity) swapView.getContext();
|
||||
}
|
||||
|
||||
if (activity != null) {
|
||||
activity.startActivityForResult(intent, MainActivity.REQUEST_STORAGE_ACCESS);
|
||||
} else {
|
||||
// scan in the background without requesting permissions
|
||||
Toast.makeText(context.getApplicationContext(),
|
||||
context.getString(R.string.scan_removable_storage_toast, externalStorage),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
SDCardScannerService.scan(context);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user