1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.network.apn; 18 19 import static com.android.settings.network.apn.ApnEditPageProviderKt.EDIT_URL; 20 import static com.android.settings.network.apn.ApnEditPageProviderKt.INSERT_URL; 21 22 import android.app.settings.SettingsEnums; 23 import android.content.Intent; 24 import android.net.Uri; 25 import android.os.Bundle; 26 import android.os.UserManager; 27 import android.provider.Telephony; 28 import android.telephony.SubscriptionManager; 29 import android.util.Log; 30 31 import androidx.annotation.VisibleForTesting; 32 33 import com.android.settings.SettingsPreferenceFragment; 34 import com.android.settings.spa.SpaActivity; 35 36 /** Use to edit apn settings. */ 37 public class ApnEditor extends SettingsPreferenceFragment { 38 39 private static final String TAG = ApnEditor.class.getSimpleName(); 40 41 @Override onCreate(Bundle icicle)42 public void onCreate(Bundle icicle) { 43 super.onCreate(icicle); 44 maybeRedirectToNewPage(); 45 finish(); 46 } 47 maybeRedirectToNewPage()48 private void maybeRedirectToNewPage() { 49 if (isUserRestricted()) { 50 Log.e(TAG, "This setting isn't available due to user restriction."); 51 return; 52 } 53 54 final Intent intent = getIntent(); 55 final String action = intent.getAction(); 56 57 int subId = 58 intent.getIntExtra(ApnSettings.SUB_ID, SubscriptionManager.INVALID_SUBSCRIPTION_ID); 59 60 Uri uri = intent.getData(); 61 if (Intent.ACTION_EDIT.equals(action)) { 62 if (!uri.isPathPrefixMatch(Telephony.Carriers.CONTENT_URI)) { 63 Log.e(TAG, "Edit request not for carrier table. Uri: " + uri); 64 } else { 65 String route = ApnEditPageProvider.INSTANCE.getRoute(EDIT_URL, uri, subId); 66 SpaActivity.startSpaActivity(requireContext(), route); 67 } 68 } else if (Intent.ACTION_INSERT.equals(action)) { 69 if (!uri.isPathPrefixMatch(Telephony.Carriers.CONTENT_URI)) { 70 Log.e(TAG, "Insert request not for carrier table. Uri: " + uri); 71 } else { 72 String route = ApnEditPageProvider.INSTANCE.getRoute( 73 INSERT_URL, Telephony.Carriers.CONTENT_URI, subId); 74 SpaActivity.startSpaActivity(getContext(), route); 75 } 76 } 77 } 78 79 @Override getMetricsCategory()80 public int getMetricsCategory() { 81 return SettingsEnums.APN_EDITOR; 82 } 83 84 @VisibleForTesting isUserRestricted()85 boolean isUserRestricted() { 86 UserManager userManager = getContext().getSystemService(UserManager.class); 87 if (userManager == null) { 88 return false; 89 } 90 if (!userManager.isAdminUser()) { 91 Log.e(TAG, "User is not an admin"); 92 return true; 93 } 94 if (userManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) { 95 Log.e(TAG, "User is not allowed to configure mobile network"); 96 return true; 97 } 98 return false; 99 } 100 } 101