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