• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.phone;
18 
19 import android.content.Context;
20 import android.graphics.Color;
21 import android.graphics.drawable.ColorDrawable;
22 import android.graphics.drawable.Drawable;
23 import android.graphics.drawable.LayerDrawable;
24 import android.preference.Preference;
25 import android.telephony.CellInfo;
26 import android.telephony.SignalStrength;
27 import android.telephony.TelephonyManager;
28 import android.util.Log;
29 import android.view.Gravity;
30 
31 import com.android.settingslib.graph.SignalDrawable;
32 
33 import java.util.List;
34 
35 /**
36  * A Preference represents a network operator in the NetworkSelectSetting fragment.
37  */
38 public class NetworkOperatorPreference extends Preference {
39 
40     private static final String TAG = "NetworkOperatorPref";
41     private static final boolean DBG = false;
42     // number of signal strength level
43     public static final int NUMBER_OF_LEVELS = SignalStrength.NUM_SIGNAL_STRENGTH_BINS;
44     private CellInfo mCellInfo;
45     private List<String> mForbiddenPlmns;
46     private int mLevel = -1;
47 
48     // The following constants are used to draw signal icon.
49     private static final Drawable EMPTY_DRAWABLE = new ColorDrawable(Color.TRANSPARENT);
50     private static final int NO_CELL_DATA_CONNECTED_ICON = 0;
51 
NetworkOperatorPreference( CellInfo cellinfo, Context context, List<String> forbiddenPlmns)52     public NetworkOperatorPreference(
53             CellInfo cellinfo, Context context, List<String> forbiddenPlmns) {
54         super(context);
55         mCellInfo = cellinfo;
56         mForbiddenPlmns = forbiddenPlmns;
57         refresh();
58     }
59 
getCellInfo()60     public CellInfo getCellInfo() {
61         return mCellInfo;
62     }
63 
64     /**
65      * Refresh the NetworkOperatorPreference by updating the title and the icon.
66      */
refresh()67     public void refresh() {
68         if (DBG) Log.d(TAG, "refresh the network: " + CellInfoUtil.getNetworkTitle(mCellInfo));
69         String networkTitle = CellInfoUtil.getNetworkTitle(mCellInfo);
70         if (CellInfoUtil.isForbidden(mCellInfo, mForbiddenPlmns)) {
71             networkTitle += " " + getContext().getResources().getString(R.string.forbidden_network);
72         }
73         setTitle(networkTitle);
74         int level = CellInfoUtil.getLevel(mCellInfo);
75         if (DBG) Log.d(TAG, "refresh level: " + String.valueOf(level));
76         if (mLevel != level) {
77             mLevel = level;
78             updateIcon(mLevel);
79         }
80     }
81 
82     /**
83      * Update the icon according to the input signal strength level.
84      */
setIcon(int level)85     public void setIcon(int level) {
86         updateIcon(level);
87     }
88 
getIconId(int networkType)89     private int getIconId(int networkType) {
90         if (networkType == TelephonyManager.NETWORK_TYPE_CDMA) {
91             return R.drawable.signal_strength_1x;
92         } else if (networkType == TelephonyManager.NETWORK_TYPE_LTE) {
93             return R.drawable.signal_strength_lte;
94         } else if (networkType == TelephonyManager.NETWORK_TYPE_UMTS) {
95             return R.drawable.signal_strength_3g;
96         } else if (networkType == TelephonyManager.NETWORK_TYPE_GSM) {
97             return R.drawable.signal_strength_g;
98         } else {
99             return 0;
100         }
101     }
102 
updateIcon(int level)103     private void updateIcon(int level) {
104         if (level < 0 || level >= NUMBER_OF_LEVELS) return;
105         Context context = getContext();
106         // Make the signal strength drawable
107         int iconId = 0;
108         if (DBG) Log.d(TAG, "updateIcon level: " + String.valueOf(level));
109         iconId = SignalDrawable.getState(level, NUMBER_OF_LEVELS, false /* cutOut */);
110 
111         SignalDrawable signalDrawable = new SignalDrawable(getContext());
112         signalDrawable.setLevel(iconId);
113         signalDrawable.setDarkIntensity(0);
114 
115         // Make the network type drawable
116         int iconType = getIconId(CellInfoUtil.getNetworkType(mCellInfo));
117         Drawable networkDrawable =
118                 iconType == NO_CELL_DATA_CONNECTED_ICON
119                         ? EMPTY_DRAWABLE
120                         : getContext()
121                         .getResources().getDrawable(iconType, getContext().getTheme());
122 
123         // Overlay the two drawables
124         Drawable[] layers = {networkDrawable, signalDrawable};
125         final int iconSize =
126                 context.getResources().getDimensionPixelSize(R.dimen.signal_strength_icon_size);
127 
128         LayerDrawable icons = new LayerDrawable(layers);
129         // Set the network type icon at the top left
130         icons.setLayerGravity(0 /* index of networkDrawable */, Gravity.TOP | Gravity.LEFT);
131         // Set the signal strength icon at the bottom right
132         icons.setLayerGravity(1 /* index of SignalDrawable */, Gravity.BOTTOM | Gravity.RIGHT);
133         icons.setLayerSize(1 /* index of SignalDrawable */, iconSize, iconSize);
134         setIcon(icons);
135     }
136 }
137