Merge "Add support for Predictive Back in WorkLockActivity" into tm-qpr-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
dabfa35320
@@ -33,6 +33,8 @@ import android.os.Bundle;
|
|||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
|
import android.window.OnBackInvokedCallback;
|
||||||
|
import android.window.OnBackInvokedDispatcher;
|
||||||
|
|
||||||
import com.android.internal.annotations.VisibleForTesting;
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
import com.android.systemui.R;
|
import com.android.systemui.R;
|
||||||
@@ -61,6 +63,7 @@ public class WorkLockActivity extends Activity {
|
|||||||
private UserManager mUserManager;
|
private UserManager mUserManager;
|
||||||
private PackageManager mPackageManager;
|
private PackageManager mPackageManager;
|
||||||
private final BroadcastDispatcher mBroadcastDispatcher;
|
private final BroadcastDispatcher mBroadcastDispatcher;
|
||||||
|
private final OnBackInvokedCallback mBackCallback = this::onBackInvoked;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public WorkLockActivity(BroadcastDispatcher broadcastDispatcher, UserManager userManager,
|
public WorkLockActivity(BroadcastDispatcher broadcastDispatcher, UserManager userManager,
|
||||||
@@ -95,6 +98,10 @@ public class WorkLockActivity extends Activity {
|
|||||||
if (badgedIcon != null) {
|
if (badgedIcon != null) {
|
||||||
((ImageView) findViewById(R.id.icon)).setImageDrawable(badgedIcon);
|
((ImageView) findViewById(R.id.icon)).setImageDrawable(badgedIcon);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
|
||||||
|
OnBackInvokedDispatcher.PRIORITY_DEFAULT,
|
||||||
|
mBackCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -134,11 +141,16 @@ public class WorkLockActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
unregisterBroadcastReceiver();
|
unregisterBroadcastReceiver();
|
||||||
|
getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(mBackCallback);
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBackPressed() {
|
public void onBackPressed() {
|
||||||
|
onBackInvoked();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onBackInvoked() {
|
||||||
// Ignore back presses.
|
// Ignore back presses.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,106 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2017 The Android Open Source Project
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.android.systemui.keyguard;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.Mockito.eq;
|
|
||||||
import static org.mockito.Mockito.never;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import android.annotation.UserIdInt;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.pm.ApplicationInfo;
|
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
import android.os.Looper;
|
|
||||||
import android.os.UserHandle;
|
|
||||||
import android.os.UserManager;
|
|
||||||
|
|
||||||
import androidx.test.filters.SmallTest;
|
|
||||||
import androidx.test.runner.AndroidJUnit4;
|
|
||||||
|
|
||||||
import com.android.systemui.SysuiTestCase;
|
|
||||||
import com.android.systemui.broadcast.BroadcastDispatcher;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* runtest systemui -c com.android.systemui.keyguard.WorkLockActivityTest
|
|
||||||
*/
|
|
||||||
@SmallTest
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
|
||||||
public class WorkLockActivityTest extends SysuiTestCase {
|
|
||||||
private static final @UserIdInt int USER_ID = 270;
|
|
||||||
private static final String CALLING_PACKAGE_NAME = "com.android.test";
|
|
||||||
|
|
||||||
private @Mock UserManager mUserManager;
|
|
||||||
private @Mock PackageManager mPackageManager;
|
|
||||||
private @Mock Context mContext;
|
|
||||||
private @Mock BroadcastDispatcher mBroadcastDispatcher;
|
|
||||||
private @Mock Drawable mDrawable;
|
|
||||||
private @Mock Drawable mBadgedDrawable;
|
|
||||||
|
|
||||||
private WorkLockActivity mActivity;
|
|
||||||
|
|
||||||
private static class WorkLockActivityTestable extends WorkLockActivity {
|
|
||||||
WorkLockActivityTestable(Context baseContext, BroadcastDispatcher broadcastDispatcher,
|
|
||||||
UserManager userManager, PackageManager packageManager) {
|
|
||||||
super(broadcastDispatcher, userManager, packageManager);
|
|
||||||
attachBaseContext(baseContext);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() throws Exception {
|
|
||||||
MockitoAnnotations.initMocks(this);
|
|
||||||
|
|
||||||
if (Looper.myLooper() == null) {
|
|
||||||
Looper.prepare();
|
|
||||||
}
|
|
||||||
mActivity = new WorkLockActivityTestable(mContext, mBroadcastDispatcher, mUserManager,
|
|
||||||
mPackageManager);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetBadgedIcon() throws Exception {
|
|
||||||
ApplicationInfo info = new ApplicationInfo();
|
|
||||||
when(mPackageManager.getApplicationInfoAsUser(eq(CALLING_PACKAGE_NAME), any(),
|
|
||||||
eq(USER_ID))).thenReturn(info);
|
|
||||||
when(mPackageManager.getApplicationIcon(eq(info))).thenReturn(mDrawable);
|
|
||||||
when(mUserManager.getBadgedIconForUser(any(), eq(UserHandle.of(USER_ID)))).thenReturn(
|
|
||||||
mBadgedDrawable);
|
|
||||||
mActivity.setIntent(new Intent()
|
|
||||||
.putExtra(Intent.EXTRA_USER_ID, USER_ID)
|
|
||||||
.putExtra(Intent.EXTRA_PACKAGE_NAME, CALLING_PACKAGE_NAME));
|
|
||||||
|
|
||||||
assertEquals(mBadgedDrawable, mActivity.getBadgedIcon());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUnregisteredFromDispatcher() {
|
|
||||||
mActivity.unregisterBroadcastReceiver();
|
|
||||||
verify(mBroadcastDispatcher).unregisterReceiver(any());
|
|
||||||
verify(mContext, never()).unregisterReceiver(any());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License
|
||||||
|
*/
|
||||||
|
package com.android.systemui.keyguard
|
||||||
|
|
||||||
|
import android.annotation.UserIdInt
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.pm.ApplicationInfo
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.content.pm.PackageManager.ApplicationInfoFlags
|
||||||
|
import android.graphics.drawable.Drawable
|
||||||
|
import android.os.Looper
|
||||||
|
import android.os.UserHandle
|
||||||
|
import android.os.UserManager
|
||||||
|
import androidx.test.filters.SmallTest
|
||||||
|
import androidx.test.runner.AndroidJUnit4
|
||||||
|
import com.android.systemui.SysuiTestCase
|
||||||
|
import com.android.systemui.broadcast.BroadcastDispatcher
|
||||||
|
import com.android.systemui.util.mockito.any
|
||||||
|
import com.android.systemui.util.mockito.eq
|
||||||
|
import com.android.systemui.util.mockito.mock
|
||||||
|
import com.android.systemui.util.mockito.nullable
|
||||||
|
import com.android.systemui.util.mockito.whenever
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.ArgumentMatchers
|
||||||
|
import org.mockito.Mockito.never
|
||||||
|
import org.mockito.Mockito.verify
|
||||||
|
|
||||||
|
/** runtest systemui -c com.android.systemui.keyguard.WorkLockActivityTest */
|
||||||
|
@SmallTest
|
||||||
|
@RunWith(AndroidJUnit4::class)
|
||||||
|
class WorkLockActivityTest : SysuiTestCase() {
|
||||||
|
private val context: Context = mock()
|
||||||
|
private val userManager: UserManager = mock()
|
||||||
|
private val packageManager: PackageManager = mock()
|
||||||
|
private val broadcastDispatcher: BroadcastDispatcher = mock()
|
||||||
|
private val drawable: Drawable = mock()
|
||||||
|
private val badgedDrawable: Drawable = mock()
|
||||||
|
private lateinit var activity: WorkLockActivity
|
||||||
|
|
||||||
|
private class WorkLockActivityTestable
|
||||||
|
constructor(
|
||||||
|
baseContext: Context,
|
||||||
|
broadcastDispatcher: BroadcastDispatcher,
|
||||||
|
userManager: UserManager,
|
||||||
|
packageManager: PackageManager,
|
||||||
|
) : WorkLockActivity(broadcastDispatcher, userManager, packageManager) {
|
||||||
|
init {
|
||||||
|
attachBaseContext(baseContext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setUp() {
|
||||||
|
if (Looper.myLooper() == null) {
|
||||||
|
Looper.prepare()
|
||||||
|
}
|
||||||
|
activity =
|
||||||
|
WorkLockActivityTestable(
|
||||||
|
baseContext = context,
|
||||||
|
broadcastDispatcher = broadcastDispatcher,
|
||||||
|
userManager = userManager,
|
||||||
|
packageManager = packageManager
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetBadgedIcon() {
|
||||||
|
val info = ApplicationInfo()
|
||||||
|
whenever(
|
||||||
|
packageManager.getApplicationInfoAsUser(
|
||||||
|
eq(CALLING_PACKAGE_NAME),
|
||||||
|
any<ApplicationInfoFlags>(),
|
||||||
|
eq(USER_ID)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.thenReturn(info)
|
||||||
|
whenever(packageManager.getApplicationIcon(ArgumentMatchers.eq(info))).thenReturn(drawable)
|
||||||
|
whenever(userManager.getBadgedIconForUser(any(), eq(UserHandle.of(USER_ID))))
|
||||||
|
.thenReturn(badgedDrawable)
|
||||||
|
activity.intent =
|
||||||
|
Intent()
|
||||||
|
.putExtra(Intent.EXTRA_USER_ID, USER_ID)
|
||||||
|
.putExtra(Intent.EXTRA_PACKAGE_NAME, CALLING_PACKAGE_NAME)
|
||||||
|
assertEquals(badgedDrawable, activity.badgedIcon)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testUnregisteredFromDispatcher() {
|
||||||
|
activity.unregisterBroadcastReceiver()
|
||||||
|
verify(broadcastDispatcher).unregisterReceiver(any())
|
||||||
|
verify(context, never()).unregisterReceiver(nullable())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun onBackPressed_finishActivity() {
|
||||||
|
assertFalse(activity.isFinishing)
|
||||||
|
|
||||||
|
activity.onBackPressed()
|
||||||
|
|
||||||
|
assertFalse(activity.isFinishing)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@UserIdInt private val USER_ID = 270
|
||||||
|
private const val CALLING_PACKAGE_NAME = "com.android.test"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user