Handle USB-OTG support differently
* Use separate receivers instead of one combined activity to avoid showing the "Use F-Droid to handle Mass Storage" prompt every time a drive is plugged in.
This commit is contained in:
parent
881a7e59c6
commit
6c4315f9a1
@ -1,9 +1,9 @@
|
|||||||
package org.fdroid.fdroid.views.main;
|
package org.fdroid.fdroid.views.main;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.content.Context;
|
||||||
|
|
||||||
class NearbyViewBinder {
|
class NearbyViewBinder {
|
||||||
public static void updateUsbOtg(final Activity activity) {
|
public static void updateUsbOtg(Context context) {
|
||||||
throw new IllegalStateException("unimplemented");
|
throw new IllegalStateException("unimplemented");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,16 +91,25 @@
|
|||||||
android:name=".nearby.SDCardScannerService"
|
android:name=".nearby.SDCardScannerService"
|
||||||
android:exported="false"/>
|
android:exported="false"/>
|
||||||
|
|
||||||
<activity
|
<receiver
|
||||||
android:name=".nearby.UsbDeviceAttachedActivity"
|
android:name=".nearby.UsbDeviceAttachedReceiver">
|
||||||
android:noHistory="true">
|
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
|
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
<meta-data
|
<meta-data
|
||||||
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
|
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
|
||||||
android:resource="@xml/device_filter"/>
|
android:resource="@xml/device_filter"/>
|
||||||
</activity>
|
</receiver>
|
||||||
|
|
||||||
|
<receiver
|
||||||
|
android:name=".nearby.UsbDeviceDetachedReceiver">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/>
|
||||||
|
</intent-filter>
|
||||||
|
<meta-data
|
||||||
|
android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
|
||||||
|
android:resource="@xml/device_filter"/>
|
||||||
|
</receiver>
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".panic.PanicPreferencesActivity"
|
android:name=".panic.PanicPreferencesActivity"
|
||||||
|
@ -95,20 +95,20 @@ public class TreeUriScannerIntentService extends IntentService {
|
|||||||
* {@link TreeUriScannerIntentService} or whether it is External Storage
|
* {@link TreeUriScannerIntentService} or whether it is External Storage
|
||||||
* like an SD Card that can be directly accessed via the file system.
|
* like an SD Card that can be directly accessed via the file system.
|
||||||
*/
|
*/
|
||||||
public static void onActivityResult(Activity activity, Intent intent) {
|
public static void onActivityResult(Context context, Intent intent) {
|
||||||
if (intent == null) {
|
if (intent == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Uri uri = intent.getData();
|
Uri uri = intent.getData();
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
if (Build.VERSION.SDK_INT >= 19) {
|
if (Build.VERSION.SDK_INT >= 19) {
|
||||||
ContentResolver contentResolver = activity.getContentResolver();
|
ContentResolver contentResolver = context.getContentResolver();
|
||||||
int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
|
int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
|
||||||
contentResolver.takePersistableUriPermission(uri, perms);
|
contentResolver.takePersistableUriPermission(uri, perms);
|
||||||
}
|
}
|
||||||
String msg = String.format(activity.getString(R.string.swap_toast_using_path), uri.toString());
|
String msg = String.format(context.getString(R.string.swap_toast_using_path), uri.toString());
|
||||||
Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
|
||||||
scan(activity, uri);
|
scan(context, uri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,47 +44,28 @@ import java.util.HashMap;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This is just a shim to receive {@link UsbManager#ACTION_USB_ACCESSORY_ATTACHED}
|
* 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 class UsbDeviceAttachedReceiver extends BroadcastReceiver {
|
||||||
public static final String TAG = "UsbDeviceAttachedActivi";
|
public static final String TAG = "UsbDeviceAttachedReceiv";
|
||||||
|
|
||||||
private static final HashMap<Uri, ContentObserver> contentObservers = new HashMap<>();
|
private static final HashMap<Uri, ContentObserver> contentObservers = new HashMap<>();
|
||||||
|
|
||||||
@RequiresApi(api = 19)
|
@RequiresApi(api = 19)
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
public void onReceive(final Context context, Intent intent) {
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT < 19) {
|
if (Build.VERSION.SDK_INT < 19) {
|
||||||
finish();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Intent intent = getIntent();
|
|
||||||
if (intent == null || TextUtils.isEmpty(intent.getAction())
|
if (intent == null || TextUtils.isEmpty(intent.getAction())
|
||||||
|| !UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
|
|| !UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
|
||||||
Log.i(TAG, "ignoring irrelevant intent: " + intent);
|
Log.i(TAG, "ignoring irrelevant intent: " + intent);
|
||||||
finish();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Log.i(TAG, "handling intent: " + intent);
|
Log.i(TAG, "handling intent: " + intent);
|
||||||
|
|
||||||
final ContentResolver contentResolver = getContentResolver();
|
final ContentResolver contentResolver = context.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));
|
|
||||||
|
|
||||||
for (final UriPermission uriPermission : contentResolver.getPersistedUriPermissions()) {
|
for (final UriPermission uriPermission : contentResolver.getPersistedUriPermissions()) {
|
||||||
Uri uri = uriPermission.getUri();
|
Uri uri = uriPermission.getUri();
|
||||||
@ -92,21 +73,10 @@ public class UsbDeviceAttachedActivity extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onChange(boolean selfChange, Uri uri) {
|
public void onChange(boolean selfChange, Uri uri) {
|
||||||
NearbyViewBinder.updateUsbOtg(UsbDeviceAttachedActivity.this);
|
NearbyViewBinder.updateUsbOtg(context);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
contentResolver.registerContentObserver(uri, true, contentObserver);
|
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018-2019 Hans-Christoph Steiner <hans@eds.org>
|
||||||
|
*
|
||||||
|
* 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<Uri, ContentObserver> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -146,7 +146,7 @@ public class NearbyViewBinder {
|
|||||||
updateUsbOtg(activity);
|
updateUsbOtg(activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateUsbOtg(final Activity activity) {
|
public static void updateUsbOtg(final Context context) {
|
||||||
if (Build.VERSION.SDK_INT < 24) {
|
if (Build.VERSION.SDK_INT < 24) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -159,7 +159,7 @@ public class NearbyViewBinder {
|
|||||||
storageVolumeText.setVisibility(View.GONE);
|
storageVolumeText.setVisibility(View.GONE);
|
||||||
requestStorageVolume.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()) {
|
for (final StorageVolume storageVolume : storageManager.getStorageVolumes()) {
|
||||||
if (storageVolume.isRemovable() && !storageVolume.isPrimary()) {
|
if (storageVolume.isRemovable() && !storageVolume.isPrimary()) {
|
||||||
Log.i(TAG, "StorageVolume: " + storageVolume);
|
Log.i(TAG, "StorageVolume: " + storageVolume);
|
||||||
@ -170,13 +170,13 @@ public class NearbyViewBinder {
|
|||||||
}
|
}
|
||||||
storageVolumeText.setVisibility(View.VISIBLE);
|
storageVolumeText.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
String text = storageVolume.getDescription(activity);
|
String text = storageVolume.getDescription(context);
|
||||||
if (!TextUtils.isEmpty(text)) {
|
if (!TextUtils.isEmpty(text)) {
|
||||||
requestStorageVolume.setText(text);
|
requestStorageVolume.setText(text);
|
||||||
UsbDevice usb = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
UsbDevice usb = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
|
||||||
if (usb != null) {
|
if (usb != null) {
|
||||||
text = String.format("%s (%s %s)", text, usb.getManufacturerName(), usb.getProductName());
|
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
|
@Override
|
||||||
@RequiresApi(api = 24)
|
@RequiresApi(api = 24)
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
List<UriPermission> list = activity.getContentResolver().getPersistedUriPermissions();
|
List<UriPermission> list = context.getContentResolver().getPersistedUriPermissions();
|
||||||
if (list != null) for (UriPermission uriPermission : list) {
|
if (list != null) for (UriPermission uriPermission : list) {
|
||||||
Uri uri = uriPermission.getUri();
|
Uri uri = uriPermission.getUri();
|
||||||
if (uri.getPath().equals(String.format("/tree/%s:", storageVolume.getUuid()))) {
|
if (uri.getPath().equals(String.format("/tree/%s:", storageVolume.getUuid()))) {
|
||||||
intent.setData(uri);
|
intent.setData(uri);
|
||||||
TreeUriScannerIntentService.onActivityResult(activity, intent);
|
TreeUriScannerIntentService.onActivityResult(context, intent);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
activity.startActivityForResult(intent, MainActivity.REQUEST_STORAGE_ACCESS);
|
((Activity) context).startActivityForResult(intent,
|
||||||
|
MainActivity.REQUEST_STORAGE_ACCESS);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user