OSDN Git Service

am 46eb1857: (-s ours) Import translations. DO NOT MERGE
[android-x86/packages-apps-Settings.git] / src / com / android / settings / ActiveNetworkScorerDialog.java
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
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
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
72     private boolean buildDialog() {
73         if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
74             Log.i(TAG, "Can only set scorer for owner user.");
75             return false;
76         }
77         NetworkScorerAppData newScorer = NetworkScorerAppManager.getScorer(this, mNewPackageName);
78         if (newScorer == null) {
79             Log.e(TAG, "New package " + mNewPackageName + " is not a valid scorer.");
80             return false;
81         }
82
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.
87             setResult(RESULT_OK);
88             return false;
89         }
90
91         // Compose dialog.
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);
98         } else {
99             p.mMessage = getString(R.string.network_scorer_change_active_no_previous_dialog_text,
100                     newName);
101         }
102         p.mPositiveButtonText = getString(R.string.yes);
103         p.mNegativeButtonText = getString(R.string.no);
104         p.mPositiveButtonListener = this;
105         p.mNegativeButtonListener = this;
106         setupAlert();
107
108         return true;
109     }
110 }