Skip to content

Commit ca2cb18

Browse files
Wink SavilleAndroid (Google) Code Review
authored andcommitted
Merge "Suppress location information for non-active users." into jb-mr1-dev
2 parents d053554 + a12a7b3 commit ca2cb18

File tree

3 files changed

+101
-10
lines changed

3 files changed

+101
-10
lines changed

services/java/com/android/server/SystemServer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public void run() {
148148
NetworkTimeUpdateService networkTimeUpdater = null;
149149
CommonTimeManagementService commonTimeMgmtService = null;
150150
InputManagerService inputManager = null;
151+
TelephonyRegistry telephonyRegistry = null;
151152

152153
// Create a shared handler thread for UI within the system server.
153154
// This thread is used by at least the following components:
@@ -218,7 +219,8 @@ public void run() {
218219
ServiceManager.addService(Context.DISPLAY_SERVICE, display, true);
219220

220221
Slog.i(TAG, "Telephony Registry");
221-
ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
222+
telephonyRegistry = new TelephonyRegistry(context);
223+
ServiceManager.addService("telephony.registry", telephonyRegistry);
222224

223225
Slog.i(TAG, "Scheduling Policy");
224226
ServiceManager.addService(Context.SCHEDULING_POLICY_SERVICE,
@@ -844,6 +846,7 @@ public void run() {
844846
final StatusBarManagerService statusBarF = statusBar;
845847
final DreamManagerService dreamyF = dreamy;
846848
final InputManagerService inputManagerF = inputManager;
849+
final TelephonyRegistry telephonyRegistryF = telephonyRegistry;
847850

848851
// We now tell the activity manager it is okay to run third party
849852
// code. It will call back into us once it has gotten to the state
@@ -971,6 +974,11 @@ public void run() {
971974
} catch (Throwable e) {
972975
reportWtf("making InputManagerService ready", e);
973976
}
977+
try {
978+
if (telephonyRegistryF != null) telephonyRegistryF.systemReady();
979+
} catch (Throwable e) {
980+
reportWtf("making TelephonyRegistry ready", e);
981+
}
974982
}
975983
});
976984

services/java/com/android/server/TelephonyRegistry.java

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@
1616

1717
package com.android.server;
1818

19+
import android.app.ActivityManager;
20+
import android.content.BroadcastReceiver;
1921
import android.content.Context;
2022
import android.content.Intent;
23+
import android.content.IntentFilter;
2124
import android.content.pm.PackageManager;
2225
import android.net.LinkCapabilities;
2326
import android.net.LinkProperties;
2427
import android.os.Binder;
2528
import android.os.Bundle;
29+
import android.os.Handler;
2630
import android.os.IBinder;
31+
import android.os.Message;
2732
import android.os.RemoteException;
2833
import android.os.UserHandle;
2934
import android.telephony.CellLocation;
@@ -39,13 +44,11 @@
3944
import java.util.List;
4045
import java.io.FileDescriptor;
4146
import java.io.PrintWriter;
42-
import java.net.NetworkInterface;
4347

4448
import com.android.internal.app.IBatteryStats;
4549
import com.android.internal.telephony.ITelephonyRegistry;
4650
import com.android.internal.telephony.IPhoneStateListener;
4751
import com.android.internal.telephony.DefaultPhoneNotifier;
48-
import com.android.internal.telephony.Phone;
4952
import com.android.internal.telephony.PhoneConstants;
5053
import com.android.internal.telephony.ServiceStateTracker;
5154
import com.android.internal.telephony.TelephonyIntents;
@@ -58,6 +61,7 @@
5861
class 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
}

telephony/java/android/telephony/TelephonyManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public String getDeviceId() {
220220
public CellLocation getCellLocation() {
221221
try {
222222
Bundle bundle = getITelephony().getCellLocation();
223+
if (bundle.isEmpty()) return null;
223224
CellLocation cl = CellLocation.newFromBundle(bundle);
224225
if (cl.isEmpty())
225226
return null;

0 commit comments

Comments
 (0)