1616
1717package com .android .server ;
1818
19+ import android .app .ActivityManager ;
20+ import android .content .BroadcastReceiver ;
1921import android .content .Context ;
2022import android .content .Intent ;
23+ import android .content .IntentFilter ;
2124import android .content .pm .PackageManager ;
2225import android .net .LinkCapabilities ;
2326import android .net .LinkProperties ;
2427import android .os .Binder ;
2528import android .os .Bundle ;
29+ import android .os .Handler ;
2630import android .os .IBinder ;
31+ import android .os .Message ;
2732import android .os .RemoteException ;
2833import android .os .UserHandle ;
2934import android .telephony .CellLocation ;
3944import java .util .List ;
4045import java .io .FileDescriptor ;
4146import java .io .PrintWriter ;
42- import java .net .NetworkInterface ;
4347
4448import com .android .internal .app .IBatteryStats ;
4549import com .android .internal .telephony .ITelephonyRegistry ;
4650import com .android .internal .telephony .IPhoneStateListener ;
4751import com .android .internal .telephony .DefaultPhoneNotifier ;
48- import com .android .internal .telephony .Phone ;
4952import com .android .internal .telephony .PhoneConstants ;
5053import com .android .internal .telephony .ServiceStateTracker ;
5154import com .android .internal .telephony .TelephonyIntents ;
5861class TelephonyRegistry extends ITelephonyRegistry .Stub {
5962 private static final String TAG = "TelephonyRegistry" ;
6063 private static final boolean DBG = false ;
64+ private static final boolean DBG_LOC = false ;
6165
6266 private static class Record {
6367 String pkgForDebug ;
@@ -66,7 +70,15 @@ private static class Record {
6670
6771 IPhoneStateListener callback ;
6872
73+ int callerUid ;
74+
6975 int events ;
76+
77+ @ Override
78+ public String toString () {
79+ return "{pkgForDebug=" + pkgForDebug + " callerUid=" + callerUid +
80+ " events=" + Integer .toHexString (events ) + "}" ;
81+ }
7082 }
7183
7284 private final Context mContext ;
@@ -120,6 +132,32 @@ private static class Record {
120132 PhoneStateListener .LISTEN_DATA_CONNECTION_STATE |
121133 PhoneStateListener .LISTEN_MESSAGE_WAITING_INDICATOR ;
122134
135+ private static final int MSG_USER_SWITCHED = 1 ;
136+
137+ private final Handler mHandler = new Handler () {
138+ @ Override
139+ public void handleMessage (Message msg ) {
140+ switch (msg .what ) {
141+ case MSG_USER_SWITCHED : {
142+ Slog .d (TAG , "MSG_USER_SWITCHED userId=" + msg .arg1 );
143+ TelephonyRegistry .this .notifyCellLocation (mCellLocation );
144+ break ;
145+ }
146+ }
147+ }
148+ };
149+
150+ private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver () {
151+ @ Override
152+ public void onReceive (Context context , Intent intent ) {
153+ String action = intent .getAction ();
154+ if (Intent .ACTION_USER_SWITCHED .equals (action )) {
155+ mHandler .sendMessage (mHandler .obtainMessage (MSG_USER_SWITCHED ,
156+ intent .getIntExtra (Intent .EXTRA_USER_HANDLE , 0 ), 0 ));
157+ }
158+ }
159+ };
160+
123161 // we keep a copy of all of the state so we can send it out when folks
124162 // register for it
125163 //
@@ -140,10 +178,24 @@ private static class Record {
140178 mConnectedApns = new ArrayList <String >();
141179 }
142180
181+ public void systemReady () {
182+ // Watch for interesting updates
183+ final IntentFilter filter = new IntentFilter ();
184+ filter .addAction (Intent .ACTION_USER_SWITCHED );
185+ filter .addAction (Intent .ACTION_USER_REMOVED );
186+ mContext .registerReceiver (mBroadcastReceiver , filter );
187+ }
188+
189+ @ Override
143190 public void listen (String pkgForDebug , IPhoneStateListener callback , int events ,
144191 boolean notifyNow ) {
145- // Slog.d(TAG, "listen pkg=" + pkgForDebug + " events=0x" +
146- // Integer.toHexString(events));
192+ int callerUid = UserHandle .getCallingUserId ();
193+ int myUid = UserHandle .myUserId ();
194+ if (DBG ) {
195+ Slog .d (TAG , "listen: E pkg=" + pkgForDebug + " events=0x" + Integer .toHexString (events )
196+ + " myUid=" + myUid
197+ + " callerUid=" + callerUid );
198+ }
147199 if (events != 0 ) {
148200 /* Checks permission and throws Security exception */
149201 checkListenerPermission (events );
@@ -164,7 +216,9 @@ public void listen(String pkgForDebug, IPhoneStateListener callback, int events,
164216 r .binder = b ;
165217 r .callback = callback ;
166218 r .pkgForDebug = pkgForDebug ;
219+ r .callerUid = callerUid ;
167220 mRecords .add (r );
221+ if (DBG ) Slog .i (TAG , "listen: add new record=" + r );
168222 }
169223 int send = events & (events ^ r .events );
170224 r .events = events ;
@@ -199,8 +253,9 @@ public void listen(String pkgForDebug, IPhoneStateListener callback, int events,
199253 remove (r .binder );
200254 }
201255 }
202- if (( events & PhoneStateListener .LISTEN_CELL_LOCATION ) != 0 ) {
256+ if (validateEventsAndUserLocked ( r , PhoneStateListener .LISTEN_CELL_LOCATION )) {
203257 try {
258+ if (DBG_LOC ) Slog .d (TAG , "listen: mCellLocation=" + mCellLocation );
204259 r .callback .onCellLocationChanged (new Bundle (mCellLocation ));
205260 } catch (RemoteException ex ) {
206261 remove (r .binder );
@@ -242,8 +297,9 @@ public void listen(String pkgForDebug, IPhoneStateListener callback, int events,
242297 remove (r .binder );
243298 }
244299 }
245- if (( events & PhoneStateListener .LISTEN_CELL_INFO ) != 0 ) {
300+ if (validateEventsAndUserLocked ( r , PhoneStateListener .LISTEN_CELL_INFO )) {
246301 try {
302+ if (DBG_LOC ) Slog .d (TAG , "listen: mCellInfo=" + mCellInfo );
247303 r .callback .onCellInfoChanged (mCellInfo );
248304 } catch (RemoteException ex ) {
249305 remove (r .binder );
@@ -346,8 +402,11 @@ public void notifyCellInfo(List<CellInfo> cellInfo) {
346402 synchronized (mRecords ) {
347403 mCellInfo = cellInfo ;
348404 for (Record r : mRecords ) {
349- if (( r . events & PhoneStateListener .LISTEN_CELL_INFO ) != 0 ) {
405+ if (validateEventsAndUserLocked ( r , PhoneStateListener .LISTEN_CELL_INFO )) {
350406 try {
407+ if (DBG_LOC ) {
408+ Slog .d (TAG , "notifyCellInfo: mCellInfo=" + mCellInfo + " r=" + r );
409+ }
351410 r .callback .onCellInfoChanged (cellInfo );
352411 } catch (RemoteException ex ) {
353412 mRemoveList .add (r .binder );
@@ -424,7 +483,8 @@ public void notifyDataConnection(int state, boolean isDataConnectivityPossible,
424483 if (DBG ) {
425484 Slog .i (TAG , "notifyDataConnection: state=" + state + " isDataConnectivityPossible="
426485 + isDataConnectivityPossible + " reason='" + reason
427- + "' apn='" + apn + "' apnType=" + apnType + " networkType=" + networkType );
486+ + "' apn='" + apn + "' apnType=" + apnType + " networkType=" + networkType
487+ + " mRecords.size()=" + mRecords .size () + " mRecords=" + mRecords );
428488 }
429489 synchronized (mRecords ) {
430490 boolean modified = false ;
@@ -506,8 +566,12 @@ public void notifyCellLocation(Bundle cellLocation) {
506566 synchronized (mRecords ) {
507567 mCellLocation = cellLocation ;
508568 for (Record r : mRecords ) {
509- if (( r . events & PhoneStateListener .LISTEN_CELL_LOCATION ) != 0 ) {
569+ if (validateEventsAndUserLocked ( r , PhoneStateListener .LISTEN_CELL_LOCATION )) {
510570 try {
571+ if (DBG_LOC ) {
572+ Slog .d (TAG , "notifyCellLocation: mCellLocation=" + mCellLocation
573+ + " r=" + r );
574+ }
511575 r .callback .onCellLocationChanged (new Bundle (cellLocation ));
512576 } catch (RemoteException ex ) {
513577 mRemoveList .add (r .binder );
@@ -712,4 +776,22 @@ private void handleRemoveListLocked() {
712776 mRemoveList .clear ();
713777 }
714778 }
779+
780+ private boolean validateEventsAndUserLocked (Record r , int events ) {
781+ int foregroundUser ;
782+ long callingIdentity = Binder .clearCallingIdentity ();
783+ boolean valid = false ;
784+ try {
785+ foregroundUser = ActivityManager .getCurrentUser ();
786+ valid = r .callerUid == foregroundUser && (r .events & events ) != 0 ;
787+ if (DBG | DBG_LOC ) {
788+ Slog .d (TAG , "validateEventsAndUserLocked: valid=" + valid
789+ + " r.callerUid=" + r .callerUid + " foregroundUser=" + foregroundUser
790+ + " r.events=" + r .events + " events=" + events );
791+ }
792+ } finally {
793+ Binder .restoreCallingIdentity (callingIdentity );
794+ }
795+ return valid ;
796+ }
715797}
0 commit comments