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:
Chirayu Desai 2020-01-24 23:52:43 +05:30
parent 881a7e59c6
commit 6c4315f9a1
6 changed files with 108 additions and 54 deletions

View File

@ -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");
}
}

View File

@ -91,16 +91,25 @@
android:name=".nearby.SDCardScannerService"
android:exported="false"/>
<activity
android:name=".nearby.UsbDeviceAttachedActivity"
android:noHistory="true">
<receiver
android:name=".nearby.UsbDeviceAttachedReceiver">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
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
android:name=".panic.PanicPreferencesActivity"

View File

@ -95,20 +95,20 @@ public class TreeUriScannerIntentService extends IntentService {
* {@link TreeUriScannerIntentService} or whether it is External Storage
* 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) {
return;
}
Uri uri = intent.getData();
if (uri != null) {
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;
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);
}
}

View File

@ -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<Uri, ContentObserver> 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();
}
}

View File

@ -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);
}
}
}

View File

@ -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<UriPermission> list = activity.getContentResolver().getPersistedUriPermissions();
List<UriPermission> 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);
}
});
}