1 /*
2  * Copyright (C) 2017 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.systemui.statusbar.phone;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.graphics.drawable.Icon;
22 import android.os.UserHandle;
23 
24 import com.android.internal.statusbar.StatusBarIcon;
25 import com.android.systemui.statusbar.phone.StatusBarSignalPolicy.MobileIconState;
26 import com.android.systemui.statusbar.phone.StatusBarSignalPolicy.WifiIconState;
27 
28 /**
29  * Wraps {@link com.android.internal.statusbar.StatusBarIcon} so we can still have a uniform list
30  */
31 public class StatusBarIconHolder {
32     public static final int TYPE_ICON = 0;
33     public static final int TYPE_WIFI = 1;
34     public static final int TYPE_MOBILE = 2;
35 
36     private StatusBarIcon mIcon;
37     private WifiIconState mWifiState;
38     private MobileIconState mMobileState;
39     private int mType = TYPE_ICON;
40     private int mTag = 0;
41     private boolean mVisible = true;
42 
fromIcon(StatusBarIcon icon)43     public static StatusBarIconHolder fromIcon(StatusBarIcon icon) {
44         StatusBarIconHolder wrapper = new StatusBarIconHolder();
45         wrapper.mIcon = icon;
46 
47         return wrapper;
48     }
49 
fromResId(Context context, int resId, CharSequence contentDescription)50     public static StatusBarIconHolder fromResId(Context context, int resId,
51             CharSequence contentDescription) {
52         StatusBarIconHolder holder = new StatusBarIconHolder();
53         holder.mIcon = new StatusBarIcon(UserHandle.SYSTEM, context.getPackageName(),
54                 Icon.createWithResource( context, resId), 0, 0, contentDescription);
55         return holder;
56     }
57 
fromWifiIconState(WifiIconState state)58     public static StatusBarIconHolder fromWifiIconState(WifiIconState state) {
59         StatusBarIconHolder holder = new StatusBarIconHolder();
60         holder.mWifiState = state;
61         holder.mType = TYPE_WIFI;
62         return holder;
63     }
64 
fromMobileIconState(MobileIconState state)65     public static StatusBarIconHolder fromMobileIconState(MobileIconState state) {
66         StatusBarIconHolder holder = new StatusBarIconHolder();
67         holder.mMobileState = state;
68         holder.mType = TYPE_MOBILE;
69         holder.mTag = state.subId;
70         return holder;
71     }
72 
getType()73     public int getType() {
74         return mType;
75     }
76 
77     @Nullable
getIcon()78     public StatusBarIcon getIcon() {
79         return mIcon;
80     }
81 
82     @Nullable
getWifiState()83     public WifiIconState getWifiState() {
84         return mWifiState;
85     }
86 
setWifiState(WifiIconState state)87     public void setWifiState(WifiIconState state) {
88         mWifiState = state;
89     }
90 
91     @Nullable
getMobileState()92     public MobileIconState getMobileState() {
93         return mMobileState;
94     }
95 
setMobileState(MobileIconState state)96     public void setMobileState(MobileIconState state) {
97         mMobileState = state;
98     }
99 
isVisible()100     public boolean isVisible() {
101         switch (mType) {
102             case TYPE_ICON:
103                 return mIcon.visible;
104             case TYPE_WIFI:
105                 return mWifiState.visible;
106             case TYPE_MOBILE:
107                 return mMobileState.visible;
108 
109             default: return true;
110         }
111     }
112 
setVisible(boolean visible)113     public void setVisible(boolean visible) {
114         if (isVisible() == visible) {
115             return;
116         }
117 
118         switch (mType) {
119             case TYPE_ICON:
120                 mIcon.visible = visible;
121                 break;
122 
123             case TYPE_WIFI:
124                 mWifiState.visible = visible;
125                 break;
126 
127             case TYPE_MOBILE:
128                 mMobileState.visible = visible;
129                 break;
130         }
131     }
132 
getTag()133     public int getTag() {
134         return mTag;
135     }
136 }
137