Merge "Clean up unused MobilePlanPreferenceController" into main
This commit is contained in:
@@ -1,193 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 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.network;
|
||||
|
||||
import static android.content.Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT;
|
||||
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
|
||||
import static android.os.UserHandle.myUserId;
|
||||
import static android.os.UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
|
||||
|
||||
import static com.android.settingslib.RestrictedLockUtilsInternal.hasBaseUserRestriction;
|
||||
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.Utils;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnCreate;
|
||||
import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MobilePlanPreferenceController extends AbstractPreferenceController
|
||||
implements PreferenceControllerMixin, LifecycleObserver, OnCreate, OnSaveInstanceState {
|
||||
|
||||
public interface MobilePlanPreferenceHost {
|
||||
void showMobilePlanMessageDialog();
|
||||
}
|
||||
|
||||
public static final int MANAGE_MOBILE_PLAN_DIALOG_ID = 1;
|
||||
|
||||
private static final String TAG = "MobilePlanPrefContr";
|
||||
static final String KEY_MANAGE_MOBILE_PLAN = "manage_mobile_plan";
|
||||
private static final String SAVED_MANAGE_MOBILE_PLAN_MSG = "mManageMobilePlanMessage";
|
||||
|
||||
private final UserManager mUserManager;
|
||||
private final boolean mIsSecondaryUser;
|
||||
private final MobilePlanPreferenceHost mHost;
|
||||
|
||||
private ConnectivityManager mCm;
|
||||
private TelephonyManager mTm;
|
||||
|
||||
private String mMobilePlanDialogMessage;
|
||||
|
||||
public MobilePlanPreferenceController(Context context,
|
||||
MobilePlanPreferenceHost host) {
|
||||
super(context);
|
||||
mHost = host;
|
||||
mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
mTm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||
mIsSecondaryUser = !mUserManager.isAdminUser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
if (mHost != null && KEY_MANAGE_MOBILE_PLAN.equals(preference.getKey())) {
|
||||
mMobilePlanDialogMessage = null;
|
||||
onManageMobilePlanClick();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
if (savedInstanceState != null) {
|
||||
mMobilePlanDialogMessage = savedInstanceState.getString(SAVED_MANAGE_MOBILE_PLAN_MSG);
|
||||
}
|
||||
Log.d(TAG, "onCreate: mMobilePlanDialogMessage=" + mMobilePlanDialogMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
if (!TextUtils.isEmpty(mMobilePlanDialogMessage)) {
|
||||
outState.putString(SAVED_MANAGE_MOBILE_PLAN_MSG, mMobilePlanDialogMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public String getMobilePlanDialogMessage() {
|
||||
return mMobilePlanDialogMessage;
|
||||
}
|
||||
|
||||
public void setMobilePlanDialogMessage(String messasge) {
|
||||
mMobilePlanDialogMessage = messasge;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
final boolean isPrefAllowedOnDevice = mContext.getResources().getBoolean(
|
||||
com.android.settings.R.bool.config_show_mobile_plan);
|
||||
final boolean isPrefAllowedForUser = !mIsSecondaryUser
|
||||
&& !Utils.isWifiOnly(mContext)
|
||||
&& !hasBaseUserRestriction(mContext, DISALLOW_CONFIG_MOBILE_NETWORKS, myUserId());
|
||||
return isPrefAllowedForUser && isPrefAllowedOnDevice;
|
||||
}
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_MANAGE_MOBILE_PLAN;
|
||||
}
|
||||
|
||||
private void onManageMobilePlanClick() {
|
||||
Resources resources = mContext.getResources();
|
||||
NetworkInfo ni = mCm.getActiveNetworkInfo();
|
||||
if (mTm.hasIccCard() && (ni != null)) {
|
||||
// Check for carrier apps that can handle provisioning first
|
||||
Intent provisioningIntent = new Intent(Intent.ACTION_CARRIER_SETUP);
|
||||
List<String> carrierPackages =
|
||||
mTm.getCarrierPackageNamesForIntent(provisioningIntent);
|
||||
if (carrierPackages != null && !carrierPackages.isEmpty()) {
|
||||
if (carrierPackages.size() != 1) {
|
||||
Log.w(TAG, "Multiple matching carrier apps found, launching the first.");
|
||||
}
|
||||
provisioningIntent.setPackage(carrierPackages.get(0));
|
||||
mContext.startActivity(provisioningIntent);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get provisioning URL
|
||||
String url = mTm.getMobileProvisioningUrl();
|
||||
if (!TextUtils.isEmpty(url)) {
|
||||
Intent intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,
|
||||
Intent.CATEGORY_APP_BROWSER);
|
||||
intent.setData(Uri.parse(url));
|
||||
intent.setFlags(FLAG_ACTIVITY_BROUGHT_TO_FRONT | FLAG_ACTIVITY_NEW_TASK);
|
||||
try {
|
||||
mContext.startActivity(intent);
|
||||
} catch (ActivityNotFoundException e) {
|
||||
Log.w(TAG, "onManageMobilePlanClick: startActivity failed" + e);
|
||||
}
|
||||
} else {
|
||||
// No provisioning URL
|
||||
String operatorName = mTm.getSimOperatorName();
|
||||
if (TextUtils.isEmpty(operatorName)) {
|
||||
// Use NetworkOperatorName as second choice in case there is no
|
||||
// SPN (Service Provider Name on the SIM). Such as with T-mobile.
|
||||
operatorName = mTm.getNetworkOperatorName();
|
||||
if (TextUtils.isEmpty(operatorName)) {
|
||||
mMobilePlanDialogMessage =
|
||||
resources.getString(R.string.mobile_unknown_sim_operator);
|
||||
} else {
|
||||
mMobilePlanDialogMessage = resources.getString(
|
||||
R.string.mobile_no_provisioning_url, operatorName);
|
||||
}
|
||||
} else {
|
||||
mMobilePlanDialogMessage =
|
||||
resources.getString(R.string.mobile_no_provisioning_url, operatorName);
|
||||
}
|
||||
}
|
||||
} else if (mTm.hasIccCard() == false) {
|
||||
// No sim card
|
||||
mMobilePlanDialogMessage = resources.getString(R.string.mobile_insert_sim_card);
|
||||
} else {
|
||||
// NetworkInfo is null, there is no connection
|
||||
mMobilePlanDialogMessage = resources.getString(R.string.mobile_connect_to_internet);
|
||||
}
|
||||
if (!TextUtils.isEmpty(mMobilePlanDialogMessage)) {
|
||||
Log.d(TAG, "onManageMobilePlanClick: message=" + mMobilePlanDialogMessage);
|
||||
if (mHost != null) {
|
||||
mHost.showMobilePlanMessageDialog();
|
||||
} else {
|
||||
Log.d(TAG, "Missing host fragment, cannot show message dialog.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,27 +15,19 @@
|
||||
*/
|
||||
package com.android.settings.network;
|
||||
|
||||
import static com.android.settings.network.MobilePlanPreferenceController.MANAGE_MOBILE_PLAN_DIALOG_ID;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsDumpService;
|
||||
import com.android.settings.core.OnActivityResultListener;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.network.MobilePlanPreferenceController.MobilePlanPreferenceHost;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.search.SearchIndexable;
|
||||
|
||||
@@ -44,7 +36,7 @@ import java.util.List;
|
||||
|
||||
@SearchIndexable
|
||||
public class NetworkDashboardFragment extends DashboardFragment implements
|
||||
MobilePlanPreferenceHost, OnActivityResultListener {
|
||||
OnActivityResultListener {
|
||||
|
||||
private static final String TAG = "NetworkDashboardFrag";
|
||||
|
||||
@@ -84,15 +76,12 @@ public class NetworkDashboardFragment extends DashboardFragment implements
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
return buildPreferenceControllers(context, getSettingsLifecycle(), mMetricsFeatureProvider,
|
||||
this /* fragment */, this /* mobilePlanHost */, this /* LifecycleOwner */);
|
||||
return buildPreferenceControllers(context, getSettingsLifecycle(),
|
||||
this /* LifecycleOwner */);
|
||||
}
|
||||
|
||||
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
|
||||
Lifecycle lifecycle, MetricsFeatureProvider metricsFeatureProvider, Fragment fragment,
|
||||
MobilePlanPreferenceHost mobilePlanHost, LifecycleOwner lifecycleOwner) {
|
||||
final MobilePlanPreferenceController mobilePlanPreferenceController =
|
||||
new MobilePlanPreferenceController(context, mobilePlanHost);
|
||||
Lifecycle lifecycle, LifecycleOwner lifecycleOwner) {
|
||||
final InternetPreferenceController internetPreferenceController =
|
||||
new InternetPreferenceController(context, lifecycle, lifecycleOwner);
|
||||
|
||||
@@ -102,7 +91,6 @@ public class NetworkDashboardFragment extends DashboardFragment implements
|
||||
new PrivateDnsPreferenceController(context);
|
||||
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(mobilePlanPreferenceController);
|
||||
lifecycle.addObserver(vpnPreferenceController);
|
||||
lifecycle.addObserver(privateDnsPreferenceController);
|
||||
}
|
||||
@@ -113,7 +101,6 @@ public class NetworkDashboardFragment extends DashboardFragment implements
|
||||
controllers.add(new TetherPreferenceController(context, lifecycle));
|
||||
controllers.add(vpnPreferenceController);
|
||||
controllers.add(new ProxyPreferenceController(context));
|
||||
controllers.add(mobilePlanPreferenceController);
|
||||
if (internetPreferenceController != null) {
|
||||
controllers.add(internetPreferenceController);
|
||||
}
|
||||
@@ -126,36 +113,6 @@ public class NetworkDashboardFragment extends DashboardFragment implements
|
||||
return controllers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showMobilePlanMessageDialog() {
|
||||
showDialog(MANAGE_MOBILE_PLAN_DIALOG_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(int dialogId) {
|
||||
Log.d(TAG, "onCreateDialog: dialogId=" + dialogId);
|
||||
switch (dialogId) {
|
||||
case MANAGE_MOBILE_PLAN_DIALOG_ID:
|
||||
final MobilePlanPreferenceController controller =
|
||||
use(MobilePlanPreferenceController.class);
|
||||
return new AlertDialog.Builder(getActivity())
|
||||
.setMessage(controller.getMobilePlanDialogMessage())
|
||||
.setCancelable(false)
|
||||
.setPositiveButton(com.android.internal.R.string.ok,
|
||||
(dialog, id) -> controller.setMobilePlanDialogMessage(null))
|
||||
.create();
|
||||
}
|
||||
return super.onCreateDialog(dialogId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDialogMetricsCategory(int dialogId) {
|
||||
if (MANAGE_MOBILE_PLAN_DIALOG_ID == dialogId) {
|
||||
return SettingsEnums.DIALOG_MANAGE_MOBILE_PLAN;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
@@ -173,20 +130,7 @@ public class NetworkDashboardFragment extends DashboardFragment implements
|
||||
public List<AbstractPreferenceController> createPreferenceControllers(Context
|
||||
context) {
|
||||
return buildPreferenceControllers(context, null /* lifecycle */,
|
||||
null /* metricsFeatureProvider */, null /* fragment */,
|
||||
null /* mobilePlanHost */, null /* LifecycleOwner */);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getNonIndexableKeys(Context context) {
|
||||
final List<String> keys = super.getNonIndexableKeys(context);
|
||||
|
||||
MobilePlanPreferenceController mppc =
|
||||
new MobilePlanPreferenceController(context, null);
|
||||
if (!mppc.isAvailable()) {
|
||||
keys.add(MobilePlanPreferenceController.KEY_MANAGE_MOBILE_PLAN);
|
||||
}
|
||||
return keys;
|
||||
null /* LifecycleOwner */);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user