1 /* 2 * Copyright (C) 2014 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; 18 19 import android.content.Context; 20 import android.content.DialogInterface; 21 import android.content.Intent; 22 import android.net.NetworkScoreManager; 23 import android.net.NetworkScorerAppManager; 24 import android.net.NetworkScorerAppManager.NetworkScorerAppData; 25 import android.os.Bundle; 26 import android.os.UserHandle; 27 import android.text.TextUtils; 28 import android.util.Log; 29 30 import com.android.internal.app.AlertActivity; 31 import com.android.internal.app.AlertController; 32 33 /** 34 * Dialog to allow a user to select a new network scorer. 35 * 36 * <p>Finishes with {@link #RESULT_CANCELED} in all circumstances unless the scorer is successfully 37 * changed or was already set to the new value (in which case it finishes with {@link #RESULT_OK}). 38 */ 39 public final class ActiveNetworkScorerDialog extends AlertActivity implements 40 DialogInterface.OnClickListener { 41 private static final String TAG = "ActiveNetScorerDlg"; 42 43 private String mNewPackageName; 44 45 @Override onCreate(Bundle savedInstanceState)46 protected void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 49 Intent intent = getIntent(); 50 mNewPackageName = intent.getStringExtra(NetworkScoreManager.EXTRA_PACKAGE_NAME); 51 52 if (!buildDialog()) { 53 finish(); 54 } 55 } 56 57 @Override onClick(DialogInterface dialog, int which)58 public void onClick(DialogInterface dialog, int which) { 59 switch (which) { 60 case BUTTON_POSITIVE: 61 NetworkScoreManager nsm = 62 (NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE); 63 if (nsm.setActiveScorer(mNewPackageName)) { 64 setResult(RESULT_OK); 65 } 66 break; 67 case BUTTON_NEGATIVE: 68 break; 69 } 70 } 71 buildDialog()72 private boolean buildDialog() { 73 // TOOD: http://b/23422763 74 if (UserHandle.myUserId() != UserHandle.USER_SYSTEM) { 75 Log.i(TAG, "Can only set scorer for owner/system user."); 76 return false; 77 } 78 NetworkScorerAppData newScorer = NetworkScorerAppManager.getScorer(this, mNewPackageName); 79 if (newScorer == null) { 80 Log.e(TAG, "New package " + mNewPackageName + " is not a valid scorer."); 81 return false; 82 } 83 84 NetworkScorerAppData oldScorer = NetworkScorerAppManager.getActiveScorer(this); 85 if (oldScorer != null && TextUtils.equals(oldScorer.mPackageName, mNewPackageName)) { 86 Log.i(TAG, "New package " + mNewPackageName + " is already the active scorer."); 87 // Set RESULT_OK to indicate to the caller that the "switch" was successful. 88 setResult(RESULT_OK); 89 return false; 90 } 91 92 // Compose dialog. 93 CharSequence newName = newScorer.mScorerName; 94 final AlertController.AlertParams p = mAlertParams; 95 p.mTitle = getString(R.string.network_scorer_change_active_dialog_title); 96 if (oldScorer != null) { 97 p.mMessage = getString(R.string.network_scorer_change_active_dialog_text, newName, 98 oldScorer.mScorerName); 99 } else { 100 p.mMessage = getString(R.string.network_scorer_change_active_no_previous_dialog_text, 101 newName); 102 } 103 p.mPositiveButtonText = getString(R.string.yes); 104 p.mNegativeButtonText = getString(R.string.no); 105 p.mPositiveButtonListener = this; 106 p.mNegativeButtonListener = this; 107 setupAlert(); 108 109 return true; 110 } 111 } 112