• 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.util.Log;
28 import android.view.Gravity;
29 
30 import com.android.settingslib.graph.SignalDrawable;
31 
32 import java.util.List;
33 
34 /**
35  * A Preference represents a network operator in the NetworkSelectSetting fragment.
36  */
37 public class NetworkOperatorPreference extends Preference {
38 
39     private static final String TAG = "NetworkOperatorPref";
40     private static final boolean DBG = false;
41     // number of signal strength level
42     public static final int NUMBER_OF_LEVELS = SignalStrength.NUM_SIGNAL_STRENGTH_BINS;
43     private CellInfo mCellInfo;
44     private List<String> mForbiddenPlmns;
45     private int mLevel = -1;
46     private boolean mShow4GForLTE;
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, boolean show4GForLTE)52     public NetworkOperatorPreference(
53             CellInfo cellinfo, Context context, List<String> forbiddenPlmns, boolean show4GForLTE) {
54         super(context);
55         mCellInfo = cellinfo;
56         mForbiddenPlmns = forbiddenPlmns;
57         mShow4GForLTE = show4GForLTE;
58         refresh();
59     }
60 
getCellInfo()61     public CellInfo getCellInfo() {
62         return mCellInfo;
63     }
64 
65     /**
66      * Refresh the NetworkOperatorPreference by updating the title and the icon.
67      */
refresh()68     public void refresh() {
69         if (DBG) Log.d(TAG, "refresh the network: " + CellInfoUtil.getNetworkTitle(mCellInfo));
70         String networkTitle = CellInfoUtil.getNetworkTitle(mCellInfo);
71         if (CellInfoUtil.isForbidden(mCellInfo, mForbiddenPlmns)) {
72             networkTitle += " " + getContext().getResources().getString(R.string.forbidden_network);
73         }
74         setTitle(networkTitle);
75         int level = mCellInfo.getCellSignalStrength().getLevel();
76         if (DBG) Log.d(TAG, "refresh level: " + String.valueOf(level));
77         if (mLevel != level) {
78             mLevel = level;
79             updateIcon(mLevel);
80         }
81     }
82 
83     /**
84      * Update the icon according to the input signal strength level.
85      */
setIcon(int level)86     public void setIcon(int level) {
87         updateIcon(level);
88     }
89 
getIconIdForCell(CellInfo ci)90     private int getIconIdForCell(CellInfo ci) {
91         final int type = ci.getCellIdentity().getType();
92         switch (type) {
93             case CellInfo.TYPE_GSM: return R.drawable.signal_strength_g;
94             case CellInfo.TYPE_WCDMA: // fall through
95             case CellInfo.TYPE_TDSCDMA: return R.drawable.signal_strength_3g;
96             case CellInfo.TYPE_LTE:
97                 return mShow4GForLTE
98                         ? R.drawable.signal_strength_4g : R.drawable.signal_strength_lte;
99             case CellInfo.TYPE_CDMA: return R.drawable.signal_strength_1x;
100             default: return 0;
101         }
102     }
103 
updateIcon(int level)104     private void updateIcon(int level) {
105         if (level < 0 || level >= NUMBER_OF_LEVELS) return;
106         Context context = getContext();
107         // Make the signal strength drawable
108         int iconId = 0;
109         if (DBG) Log.d(TAG, "updateIcon level: " + String.valueOf(level));
110         iconId = SignalDrawable.getState(level, NUMBER_OF_LEVELS, false /* cutOut */);
111 
112         SignalDrawable signalDrawable = new SignalDrawable(getContext());
113         signalDrawable.setLevel(iconId);
114         signalDrawable.setDarkIntensity(0);
115 
116         // Make the network type drawable
117         int iconType = getIconIdForCell(mCellInfo);
118         Drawable networkDrawable =
119                 iconType == NO_CELL_DATA_CONNECTED_ICON
120                         ? EMPTY_DRAWABLE
121                         : getContext()
122                         .getResources().getDrawable(iconType, getContext().getTheme());
123 
124         // Overlay the two drawables
125         Drawable[] layers = {networkDrawable, signalDrawable};
126         final int iconSize =
127                 context.getResources().getDimensionPixelSize(R.dimen.signal_strength_icon_size);
128 
129         LayerDrawable icons = new LayerDrawable(layers);
130         // Set the network type icon at the top left
131         icons.setLayerGravity(0 /* index of networkDrawable */, Gravity.TOP | Gravity.LEFT);
132         // Set the signal strength icon at the bottom right
133         icons.setLayerGravity(1 /* index of SignalDrawable */, Gravity.BOTTOM | Gravity.RIGHT);
134         icons.setLayerSize(1 /* index of SignalDrawable */, iconSize, iconSize);
135         setIcon(icons);
136     }
137 }
138