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