Merge "Add package name check for the calling app" into sc-v2-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
12b7fd54ec
@@ -16,9 +16,14 @@
|
|||||||
|
|
||||||
package com.android.settings.wifi.addappnetworks;
|
package com.android.settings.wifi.addappnetworks;
|
||||||
|
|
||||||
|
import android.app.ActivityManager;
|
||||||
|
import android.app.IActivityManager;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.RemoteException;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
@@ -48,12 +53,17 @@ public class AddAppNetworksActivity extends FragmentActivity {
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
final Bundle mBundle = new Bundle();
|
final Bundle mBundle = new Bundle();
|
||||||
|
@VisibleForTesting
|
||||||
|
IActivityManager mActivityManager = ActivityManager.getService();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.settings_panel);
|
setContentView(R.layout.settings_panel);
|
||||||
showAddNetworksFragment();
|
if (!showAddNetworksFragment()) {
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
getLifecycle().addObserver(new HideNonSystemOverlayMixin(this));
|
getLifecycle().addObserver(new HideNonSystemOverlayMixin(this));
|
||||||
|
|
||||||
// Move the window to the bottom of screen, and make it take up the entire screen width.
|
// Move the window to the bottom of screen, and make it take up the entire screen width.
|
||||||
@@ -67,13 +77,22 @@ public class AddAppNetworksActivity extends FragmentActivity {
|
|||||||
protected void onNewIntent(Intent intent) {
|
protected void onNewIntent(Intent intent) {
|
||||||
super.onNewIntent(intent);
|
super.onNewIntent(intent);
|
||||||
setIntent(intent);
|
setIntent(intent);
|
||||||
showAddNetworksFragment();
|
if (!showAddNetworksFragment()) {
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void showAddNetworksFragment() {
|
protected boolean showAddNetworksFragment() {
|
||||||
|
String packageName = getCallingAppPackageName();
|
||||||
|
if (TextUtils.isEmpty(packageName)) {
|
||||||
|
Log.d(TAG, "Package name is null");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Check the new intent status.
|
// TODO: Check the new intent status.
|
||||||
mBundle.putString(KEY_CALLING_PACKAGE_NAME, getCallingPackage());
|
mBundle.putString(KEY_CALLING_PACKAGE_NAME, packageName);
|
||||||
mBundle.putParcelableArrayList(Settings.EXTRA_WIFI_NETWORK_LIST,
|
mBundle.putParcelableArrayList(Settings.EXTRA_WIFI_NETWORK_LIST,
|
||||||
getIntent().getParcelableArrayListExtra(Settings.EXTRA_WIFI_NETWORK_LIST));
|
getIntent().getParcelableArrayListExtra(Settings.EXTRA_WIFI_NETWORK_LIST));
|
||||||
|
|
||||||
@@ -86,5 +105,19 @@ public class AddAppNetworksActivity extends FragmentActivity {
|
|||||||
} else {
|
} else {
|
||||||
((AddAppNetworksFragment) fragment).createContent(mBundle);
|
((AddAppNetworksFragment) fragment).createContent(mBundle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
protected String getCallingAppPackageName() {
|
||||||
|
String packageName;
|
||||||
|
try {
|
||||||
|
packageName = mActivityManager.getLaunchedFromPackage(getActivityToken());
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
Log.e(TAG, "Can not get the package from activity manager");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return packageName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,27 +18,70 @@ package com.android.settings.wifi.addappnetworks;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.robolectric.Shadows.shadowOf;
|
import android.annotation.Nullable;
|
||||||
|
import android.app.IActivityManager;
|
||||||
|
import android.os.RemoteException;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.Robolectric;
|
import org.robolectric.Robolectric;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class AddAppNetworksActivityTest {
|
public class AddAppNetworksActivityTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private IActivityManager mIActivityManager;
|
||||||
|
|
||||||
|
private AddAppNetworksActivity mActivity;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mActivity = Robolectric.buildActivity(AddAppNetworksActivity.class).create().get();
|
||||||
|
mActivity.mActivityManager = mIActivityManager;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void startActivity_withPackageName_bundleShouldHaveRightPackageName() {
|
public void getCallingAppPackageName_nullPackageName_returnNotNull() {
|
||||||
final String packageName = RuntimeEnvironment.application.getPackageName();
|
fakeCallingPackage("com.android.settings");
|
||||||
final AddAppNetworksActivity activity =
|
|
||||||
Robolectric.buildActivity(AddAppNetworksActivity.class).create().get();
|
|
||||||
shadowOf(activity).setCallingPackage(packageName);
|
|
||||||
|
|
||||||
activity.showAddNetworksFragment();
|
assertThat(mActivity.getCallingAppPackageName()).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
assertThat(activity.mBundle.getString(AddAppNetworksActivity.KEY_CALLING_PACKAGE_NAME))
|
@Test
|
||||||
.isEqualTo(packageName);
|
public void getCallingAppPackageName_withPackageName_returnNull() {
|
||||||
|
fakeCallingPackage(null);
|
||||||
|
|
||||||
|
assertThat(mActivity.getCallingAppPackageName()).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void showAddNetworksFragment_nullPackageName_returnFalse() {
|
||||||
|
fakeCallingPackage(null);
|
||||||
|
|
||||||
|
assertThat(mActivity.showAddNetworksFragment()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void showAddNetworksFragment_withPackageName_returnTrue() {
|
||||||
|
fakeCallingPackage("com.android.settings");
|
||||||
|
|
||||||
|
assertThat(mActivity.showAddNetworksFragment()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fakeCallingPackage(@Nullable String packageName) {
|
||||||
|
try {
|
||||||
|
when(mIActivityManager.getLaunchedFromPackage(any())).thenReturn(packageName);
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
// Do nothing.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user