Я разрабатываю Application SDK 28 с режимом LockTask для одноразового устройства. Приложение передает некоторые файлы с устройства на USB Flash Drive через USB OTG. Для связи с флэш-накопителем приложение запрашивает разрешение во время выполнения, но режим LockTask блокирует диалоговое сообщение для принятия разрешения пользователя. Из-за этого приложение не может получить разрешение, и когда оно хочет передать файлы, происходит сбой. Я хочу знать, если выход из способа включить некоторые разрешения в режиме блокировки задачи или включить эти диалоги?
Запрос разрешения на связь с Flash Drive
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void check() {
manager = (UsbManager) getSystemService(Context.USB_SERVICE);
/*
* this block required if you need to communicate to USB devices it's
* take permission to device
* if you want than you can set this to which device you want to communicate
*/
// ------------------------------------------------------------------
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
// -------------------------------------------------------------------
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
String i = "";
while (deviceIterator.hasNext()) {
device = deviceIterator.next();
manager.requestPermission(device, mPermissionIntent);
i += "USB Detectada!!";
}
}
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
UsbDevice usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (ACTION_USB_PERMISSION.equals(action)) {
// Permission requested
synchronized (this) {
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
//cueMsg.cueCorrect("Permiso otorgado");
// User has granted permission
// ... Setup your UsbDeviceConnection via mUsbManager.openDevice(usbDevice) ...
} else {
cueMsg.cueError("Permiso denegado");
// User has denied permission
}
}
}
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
cueMsg.cueError("Usb desconectado");
// Device removed
synchronized (this) {
// ... Check to see if usbDevice is yours and cleanup ...
}
}
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
cueMsg.cueCorrect("Usb conectado");
// Device attached
}
}
};
Код режима LockTask
@TargetApi(Build.VERSION_CODES.M)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onStart() {
if (mDevicePolicyManager.isLockTaskPermitted(this.getPackageName())){
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_NONE){
setDefaultCosuPolicies(true);
startLockTask();
}
}
super.onStart();
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void unlockApp(){
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
if (am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_LOCKED){
stopLockTask();
}
setDefaultCosuPolicies(false);
}
@TargetApi(Build.VERSION_CODES.M)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setDefaultCosuPolicies(boolean active) {
//Configuración de restricciones de usuario
setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, active);
setUserRestriction(UserManager.DISALLOW_FACTORY_RESET, active);
setUserRestriction(UserManager.DISALLOW_ADD_USER, active);
//setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
setUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER, false);
setUserRestriction(UsbManager.EXTRA_PERMISSION_GRANTED, true);
setUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME, active);
//Desabilitar lockscreen y barra de estado
mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, active);
mDevicePolicyManager.setStatusBarDisabled(mAdminComponentName, active);
//Permisos para USB
mDevicePolicyManager.setPermissionGrantState(mAdminComponentName,
"com.example.devicepolicymanager2",
UsbManager.EXTRA_PERMISSION_GRANTED,
DevicePolicyManager.PERMISSION_GRANT_STATE_DEFAULT);
//Habilitar Plugged in
enableStayOnWhilePluggedIn(active);
//Configurar politicas de actualización de sistema
if (active){
mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName, SystemUpdatePolicy.createWindowedInstallPolicy(60, 120));
} else {
mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName, null);
}
//Configurar la actividad como un paquete lock task
//mDevicePolicyManager.setLockTaskPackages(mAdminComponentName,
// active ? new String[]{getPackageName()} : new String[]{});
mDevicePolicyManager.setLockTaskPackages(mAdminComponentName, APP_PACKAGES);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
intentFilter.addCategory(Intent.CATEGORY_HOME);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
if (active){
//configura actividad cosu como home para que comience al reiniciarse
mDevicePolicyManager.addPersistentPreferredActivity(mAdminComponentName,
intentFilter,
new ComponentName(getPackageName(),
LoginActivity.class.getName()));
} else {
mDevicePolicyManager.clearPackagePersistentPreferredActivities(mAdminComponentName, getPackageName());
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setUserRestriction(String restriction, boolean disallow){
if (disallow){
mDevicePolicyManager.addUserRestriction(mAdminComponentName, restriction);
}else {
mDevicePolicyManager.clearUserRestriction(mAdminComponentName, restriction);
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void enableStayOnWhilePluggedIn(boolean enabled){
if (enabled){
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,
Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
Integer.toString(BatteryManager.BATTERY_PLUGGED_AC |
BatteryManager.BATTERY_PLUGGED_USB |
BatteryManager.BATTERY_PLUGGED_WIRELESS));
}else {
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,
Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
"0");
}
}