Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5145c2db authored by changbetty's avatar changbetty
Browse files

To use the getLaunchedFromPackage to get the actual package name

Bug: 170642995
Bug: 199176115
Test: Manual test
Test: atest -c RequestToggleWiFiActivityTest
Change-Id: I821e374d8762861f63185d20db2f75f05b503540
parent e5171311
Loading
Loading
Loading
Loading
+34 −15
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ package com.android.settings.wifi;
import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.IActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
@@ -29,11 +31,13 @@ import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.AlertActivity;
import com.android.settings.R;

@@ -63,6 +67,8 @@ public class RequestToggleWiFiActivity extends AlertActivity

    private @NonNull WifiManager mWiFiManager;
    private @NonNull CharSequence mAppLabel;
    @VisibleForTesting
    protected IActivityManager mActivityManager = ActivityManager.getService();

    private int mState = STATE_UNKNOWN;
    private int mLastUpdateState = STATE_UNKNOWN;
@@ -75,20 +81,8 @@ public class RequestToggleWiFiActivity extends AlertActivity

        setResult(Activity.RESULT_CANCELED);

        String packageName = getIntent().getStringExtra(Intent.EXTRA_PACKAGE_NAME);
        if (TextUtils.isEmpty(packageName)) {
            finish();
            return;
        }

        try {
            ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(
                    packageName, 0);
            mAppLabel = applicationInfo.loadSafeLabel(getPackageManager(),
                    PackageItemInfo.DEFAULT_MAX_LABEL_SIZE_PX, PackageItemInfo.SAFE_LABEL_FLAG_TRIM
                            | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(LOG_TAG, "Couldn't find app with package name " + packageName);
        mAppLabel = getAppLabel();
        if (TextUtils.isEmpty(mAppLabel)) {
            finish();
            return;
        }
@@ -140,7 +134,6 @@ public class RequestToggleWiFiActivity extends AlertActivity
    @Override
    protected void onStart() {
        super.onStart();

        mReceiver.register();

        final int wifiState = mWiFiManager.getWifiState();
@@ -223,6 +216,32 @@ public class RequestToggleWiFiActivity extends AlertActivity
        super.onStop();
    }

    @VisibleForTesting
    protected CharSequence getAppLabel() {
        String packageName;
        try {
            packageName = mActivityManager.getLaunchedFromPackage(getActivityToken());
            if (TextUtils.isEmpty(packageName)) {
                Log.d(LOG_TAG, "Package name is null");
                return null;
            }
        } catch (RemoteException e) {
            Log.e(LOG_TAG, "Can not get the package from activity manager");
            return null;
        }

        try {
            ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(
                    packageName, 0);
            return applicationInfo.loadSafeLabel(getPackageManager(),
                    PackageItemInfo.DEFAULT_MAX_LABEL_SIZE_PX, PackageItemInfo.SAFE_LABEL_FLAG_TRIM
                            | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(LOG_TAG, "Couldn't find app with package name " + packageName);
            return null;
        }
    }

    private void updateUi() {
        if (mLastUpdateState == mState) {
            return;
+101 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.settings.wifi;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import android.annotation.Nullable;
import android.app.IActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.RemoteException;

import androidx.test.core.app.ActivityScenario;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

@RunWith(AndroidJUnit4.class)
public class RequestToggleWiFiActivityTest {

    @Rule
    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
    @Spy
    private final Context mContext = ApplicationProvider.getApplicationContext();
    @Mock
    private WifiManager mWifiManager;
    @Mock
    private IActivityManager mIActivityManager;

    private ActivityScenario<RequestToggleWiFiActivity> mActivityScenario;
    private RequestToggleWiFiActivity mActivity;

    @Before
    public void setUp() {
        when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
        when(mWifiManager.getWifiState()).thenReturn(WifiManager.WIFI_STATE_DISABLED);

        mActivityScenario = ActivityScenario.launch(new Intent(WifiManager.ACTION_REQUEST_ENABLE));
        mActivityScenario.onActivity(activity -> mActivity = activity);

    }

    @After
    public void cleanUp() {
        mActivity = null;
        if (mActivityScenario != null) {
            mActivityScenario.close();
        }
    }

    @Test
    public void getAppLabel_nullPackageName_returnNull() {
        fakeCallingPackage(null);

        assertThat(mActivity.getAppLabel()).isNull();
    }

    @Test
    public void getAppLabel_settingsPackageName_returnNotNull() {
        fakeCallingPackage("com.android.settings");

        assertThat(mActivity.getAppLabel()).isNotNull();
    }

    private void fakeCallingPackage(@Nullable String packageName) {
        assertThat(mActivity).isNotNull();
        mActivity.mActivityManager = mIActivityManager;
        try {
            when(mIActivityManager.getLaunchedFromPackage(any())).thenReturn(packageName);
        } catch (RemoteException e) {
            // Do nothing.
        }
    }
}