2222import android .os .RemoteException ;
2323import android .os .IBinder ;
2424import android .os .ServiceManager ;
25+ import android .util .Slog ;
2526import android .view .View ;
2627
2728import com .android .internal .statusbar .IStatusBarService ;
@@ -61,8 +62,17 @@ public class StatusBarManager {
6162
6263 StatusBarManager (Context context ) {
6364 mContext = context ;
64- mService = IStatusBarService .Stub .asInterface (
65- ServiceManager .getService (Context .STATUS_BAR_SERVICE ));
65+ }
66+
67+ private synchronized IStatusBarService getService () {
68+ if (mService == null ) {
69+ mService = IStatusBarService .Stub .asInterface (
70+ ServiceManager .getService (Context .STATUS_BAR_SERVICE ));
71+ if (mService == null ) {
72+ Slog .w ("StatusBarManager" , "warning: no STATUS_BAR_SERVICE" );
73+ }
74+ }
75+ return mService ;
6676 }
6777
6878 /**
@@ -71,8 +81,9 @@ public class StatusBarManager {
7181 */
7282 public void disable (int what ) {
7383 try {
74- if (mService != null ) {
75- mService .disable (what , mToken , mContext .getPackageName ());
84+ final IStatusBarService svc = getService ();
85+ if (svc != null ) {
86+ svc .disable (what , mToken , mContext .getPackageName ());
7687 }
7788 } catch (RemoteException ex ) {
7889 // system process is dead anyway.
@@ -85,7 +96,10 @@ public void disable(int what) {
8596 */
8697 public void expand () {
8798 try {
88- mService .expand ();
99+ final IStatusBarService svc = getService ();
100+ if (svc != null ) {
101+ svc .expand ();
102+ }
89103 } catch (RemoteException ex ) {
90104 // system process is dead anyway.
91105 throw new RuntimeException (ex );
@@ -97,7 +111,10 @@ public void expand() {
97111 */
98112 public void collapse () {
99113 try {
100- mService .collapse ();
114+ final IStatusBarService svc = getService ();
115+ if (svc != null ) {
116+ svc .collapse ();
117+ }
101118 } catch (RemoteException ex ) {
102119 // system process is dead anyway.
103120 throw new RuntimeException (ex );
@@ -106,8 +123,11 @@ public void collapse() {
106123
107124 public void setIcon (String slot , int iconId , int iconLevel , String contentDescription ) {
108125 try {
109- mService .setIcon (slot , mContext .getPackageName (), iconId , iconLevel ,
126+ final IStatusBarService svc = getService ();
127+ if (svc != null ) {
128+ svc .setIcon (slot , mContext .getPackageName (), iconId , iconLevel ,
110129 contentDescription );
130+ }
111131 } catch (RemoteException ex ) {
112132 // system process is dead anyway.
113133 throw new RuntimeException (ex );
@@ -116,7 +136,10 @@ public void setIcon(String slot, int iconId, int iconLevel, String contentDescri
116136
117137 public void removeIcon (String slot ) {
118138 try {
119- mService .removeIcon (slot );
139+ final IStatusBarService svc = getService ();
140+ if (svc != null ) {
141+ svc .removeIcon (slot );
142+ }
120143 } catch (RemoteException ex ) {
121144 // system process is dead anyway.
122145 throw new RuntimeException (ex );
@@ -125,7 +148,10 @@ public void removeIcon(String slot) {
125148
126149 public void setIconVisibility (String slot , boolean visible ) {
127150 try {
128- mService .setIconVisibility (slot , visible );
151+ final IStatusBarService svc = getService ();
152+ if (svc != null ) {
153+ svc .setIconVisibility (slot , visible );
154+ }
129155 } catch (RemoteException ex ) {
130156 // system process is dead anyway.
131157 throw new RuntimeException (ex );
0 commit comments