2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.settings;
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;
30 import com.android.internal.app.AlertActivity;
31 import com.android.internal.app.AlertController;
34 * Dialog to allow a user to select a new network scorer.
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}).
39 public final class ActiveNetworkScorerDialog extends AlertActivity implements
40 DialogInterface.OnClickListener {
41 private static final String TAG = "ActiveNetScorerDlg";
43 private String mNewPackageName;
46 protected void onCreate(Bundle savedInstanceState) {
47 super.onCreate(savedInstanceState);
49 Intent intent = getIntent();
50 mNewPackageName = intent.getStringExtra(NetworkScoreManager.EXTRA_PACKAGE_NAME);
58 public void onClick(DialogInterface dialog, int which) {
61 NetworkScoreManager nsm =
62 (NetworkScoreManager) getSystemService(Context.NETWORK_SCORE_SERVICE);
63 if (nsm.setActiveScorer(mNewPackageName)) {
72 private boolean buildDialog() {
73 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
74 Log.i(TAG, "Can only set scorer for owner user.");
77 NetworkScorerAppData newScorer = NetworkScorerAppManager.getScorer(this, mNewPackageName);
78 if (newScorer == null) {
79 Log.e(TAG, "New package " + mNewPackageName + " is not a valid scorer.");
83 NetworkScorerAppData oldScorer = NetworkScorerAppManager.getActiveScorer(this);
84 if (oldScorer != null && TextUtils.equals(oldScorer.mPackageName, mNewPackageName)) {
85 Log.i(TAG, "New package " + mNewPackageName + " is already the active scorer.");
86 // Set RESULT_OK to indicate to the caller that the "switch" was successful.
92 CharSequence newName = newScorer.mScorerName;
93 final AlertController.AlertParams p = mAlertParams;
94 p.mTitle = getString(R.string.network_scorer_change_active_dialog_title);
95 if (oldScorer != null) {
96 p.mMessage = getString(R.string.network_scorer_change_active_dialog_text, newName,
97 oldScorer.mScorerName);
99 p.mMessage = getString(R.string.network_scorer_change_active_no_previous_dialog_text,
102 p.mPositiveButtonText = getString(R.string.yes);
103 p.mNegativeButtonText = getString(R.string.no);
104 p.mPositiveButtonListener = this;
105 p.mNegativeButtonListener = this;