下面列出了android.os.Binder#getCallingUid ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override // Binder call
public void boostScreenBrightness(long eventTime) {
if (eventTime > SystemClock.uptimeMillis()) {
throw new IllegalArgumentException("event time must not be in the future");
}
mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.DEVICE_POWER, null);
final int uid = Binder.getCallingUid();
final long ident = Binder.clearCallingIdentity();
try {
boostScreenBrightnessInternal(eventTime, uid);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
@Override
public byte[] getBackupPayload(int user) {
if (Binder.getCallingUid() != SYSTEM_UID) {
throw new SecurityException("Caller must be system");
}
//TODO: http://b/22388012
if (user != UserHandle.USER_SYSTEM) {
Slog.w(TAG, "getBackupPayload: cannot backup policy for user " + user);
return null;
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
XmlSerializer out = XmlPullParserFactory.newInstance().newSerializer();
out.setOutput(baos, Encoding.UTF_8.name());
mPermissions.writeBackup(out);
out.flush();
return baos.toByteArray();
} catch (IOException | XmlPullParserException e) {
Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e);
}
return null;
}
private ArrayList<ResolveInfo> createFilterdResolveInfoList(List<ResolveInfo> list) {
if (list == null) {
return null;
}
final ArrayList<ResolveInfo> resultList = new ArrayList<>(list.size());
final PackageManagerInternal pm = LocalServices.getService(PackageManagerInternal.class);
final int callingUid = Binder.getCallingUid();
final int callingUserId = UserHandle.getCallingUserId();
for (ResolveInfo info : list) {
if (pm.canAccessComponent(
callingUid, info.activityInfo.getComponentName(), callingUserId)) {
resultList.add(info);
}
}
return resultList;
}
void sendKeyUp(final IBinder token, final int keyCode) {
synchronized (mLock) {
if (mActiveConnection == this && Binder.getCallingUid() == mUid) {
if (DEBUG_KEY) {
Slog.d(TAG, this + ": sendKeyUp," +
" token=" + token + ", keyCode=" + keyCode);
}
final long idToken = Binder.clearCallingIdentity();
try {
if (mProviderMethods != null) {
mProviderMethods.sendKeyUp(TvRemoteProviderProxy.this, token, keyCode);
}
} finally {
Binder.restoreCallingIdentity(idToken);
}
} else {
if (DEBUG) {
Slog.w(TAG,
"sendKeyUp, Invalid connection or incorrect uid: " + Binder
.getCallingUid());
}
}
}
}
@Override
public void stopRecording(IBinder sessionToken, int userId) {
final int callingUid = Binder.getCallingUid();
final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid,
userId, "stopRecording");
final long identity = Binder.clearCallingIdentity();
try {
synchronized (mLock) {
try {
getSessionLocked(sessionToken, callingUid, resolvedUserId).stopRecording();
} catch (RemoteException | SessionNotFoundException e) {
Slog.e(TAG, "error in stopRecording", e);
}
}
} finally {
Binder.restoreCallingIdentity(identity);
}
}
@Override
public void removeOverlayView(IBinder sessionToken, int userId) {
final int callingUid = Binder.getCallingUid();
final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid,
userId, "removeOverlayView");
final long identity = Binder.clearCallingIdentity();
try {
synchronized (mLock) {
try {
getSessionLocked(sessionToken, callingUid, resolvedUserId)
.removeOverlayView();
} catch (RemoteException | SessionNotFoundException e) {
Slog.e(TAG, "error in removeOverlayView", e);
}
}
} finally {
Binder.restoreCallingIdentity(identity);
}
}
void sendTimestamp(final IBinder token, final long timestamp) {
synchronized (mLock) {
if (mActiveConnection == this && Binder.getCallingUid() == mUid) {
final long idToken = Binder.clearCallingIdentity();
try {
if (mProviderMethods != null) {
mProviderMethods.sendTimeStamp(TvRemoteProviderProxy.this, token,
timestamp);
}
} finally {
Binder.restoreCallingIdentity(idToken);
}
} else {
if (DEBUG) {
Slog.w(TAG,
"sendTimeStamp, Invalid connection or incorrect uid: " + Binder
.getCallingUid());
}
}
}
}
private void checkPackageName(String packageName) {
if (packageName == null) {
throw new SecurityException("invalid package name: " + packageName);
}
int uid = Binder.getCallingUid();
String[] packages = mPackageManager.getPackagesForUid(uid);
if (packages == null) {
throw new SecurityException("invalid UID " + uid);
}
for (String pkg : packages) {
if (packageName.equals(pkg)) return;
}
throw new SecurityException("invalid package name: " + packageName);
}
@Override
public IVoiceInteractorRequest startPickOption(String callingPackage,
IVoiceInteractorCallback callback, VoiceInteractor.Prompt prompt,
VoiceInteractor.PickOptionRequest.Option[] options, Bundle extras) {
PickOptionRequest request = new PickOptionRequest(callingPackage,
Binder.getCallingUid(), callback, VoiceInteractionSession.this,
prompt, options, extras);
addRequest(request);
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageO(MSG_START_PICK_OPTION,
request));
return request.mInterface;
}
public KeyAttestationApplicationId getKeyAttestationApplicationId(int uid)
throws RemoteException {
if (Binder.getCallingUid() != android.os.Process.KEYSTORE_UID) {
throw new SecurityException("This service can only be used by Keystore");
}
KeyAttestationPackageInfo[] keyAttestationPackageInfos = null;
final long token = Binder.clearCallingIdentity();
try {
String[] packageNames = mPackageManager.getPackagesForUid(uid);
if (packageNames == null) {
throw new RemoteException("No packages for uid");
}
int userId = UserHandle.getUserId(uid);
keyAttestationPackageInfos = new KeyAttestationPackageInfo[packageNames.length];
for (int i = 0; i < packageNames.length; ++i) {
PackageInfo packageInfo = mPackageManager.getPackageInfoAsUser(packageNames[i],
PackageManager.GET_SIGNATURES, userId);
keyAttestationPackageInfos[i] = new KeyAttestationPackageInfo(packageNames[i],
packageInfo.getLongVersionCode(), packageInfo.signatures);
}
} catch (NameNotFoundException nnfe) {
throw new RemoteException(nnfe.getMessage());
} finally {
Binder.restoreCallingIdentity(token);
}
return new KeyAttestationApplicationId(keyAttestationPackageInfos);
}
@Override
public void setVolumeTo(String packageName, ISessionControllerCallback caller,
int value, int flags) {
int pid = Binder.getCallingPid();
int uid = Binder.getCallingUid();
final long token = Binder.clearCallingIdentity();
try {
MediaSessionRecord.this.setVolumeTo(packageName, pid, uid, caller, value, flags);
} finally {
Binder.restoreCallingIdentity(token);
}
}
@Override
public void cancelRequest(SyncRequest request) {
SyncManager syncManager = getSyncManager();
if (syncManager == null) return;
int userId = UserHandle.getCallingUserId();
final int callingUid = Binder.getCallingUid();
if (request.isPeriodic()) {
mContext.enforceCallingOrSelfPermission(Manifest.permission.WRITE_SYNC_SETTINGS,
"no permission to write the sync settings");
}
Bundle extras = new Bundle(request.getBundle());
validateExtras(callingUid, extras);
long identityToken = clearCallingIdentity();
try {
SyncStorageEngine.EndPoint info;
Account account = request.getAccount();
String provider = request.getProvider();
info = new SyncStorageEngine.EndPoint(account, provider, userId);
if (request.isPeriodic()) {
// Remove periodic sync.
getSyncManager().removePeriodicSync(info, extras,
"cancelRequest() by uid=" + callingUid);
}
// Cancel active syncs and clear pending syncs from the queue.
syncManager.cancelScheduledSyncOperation(info, extras);
syncManager.cancelActiveSync(info, extras, "API");
} finally {
restoreCallingIdentity(identityToken);
}
}
@Override
public IVoiceInteractorRequest startCompleteVoice(String callingPackage,
IVoiceInteractorCallback callback, VoiceInteractor.Prompt message, Bundle extras) {
CompleteVoiceRequest request = new CompleteVoiceRequest(callingPackage,
Binder.getCallingUid(), callback, VoiceInteractionSession.this,
message, extras);
addRequest(request);
mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageO(MSG_START_COMPLETE_VOICE,
request));
return request.mInterface;
}
@Override // Binder call
public void userActivity(long eventTime, int event, int flags) {
final long now = SystemClock.uptimeMillis();
if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER)
!= PackageManager.PERMISSION_GRANTED
&& mContext.checkCallingOrSelfPermission(
android.Manifest.permission.USER_ACTIVITY)
!= PackageManager.PERMISSION_GRANTED) {
// Once upon a time applications could call userActivity().
// Now we require the DEVICE_POWER permission. Log a warning and ignore the
// request instead of throwing a SecurityException so we don't break old apps.
synchronized (mLock) {
if (now >= mLastWarningAboutUserActivityPermission + (5 * 60 * 1000)) {
mLastWarningAboutUserActivityPermission = now;
Slog.w(TAG, "Ignoring call to PowerManager.userActivity() because the "
+ "caller does not have DEVICE_POWER or USER_ACTIVITY "
+ "permission. Please fix your app! "
+ " pid=" + Binder.getCallingPid()
+ " uid=" + Binder.getCallingUid());
}
}
return;
}
if (eventTime > now) {
throw new IllegalArgumentException("event time must not be in the future");
}
final int uid = Binder.getCallingUid();
final long ident = Binder.clearCallingIdentity();
try {
userActivityInternal(eventTime, event, flags, uid);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
@Override
public void notifyCameraState(String cameraId, int newCameraState, int facing,
String clientName, int apiLevel) {
if (Binder.getCallingUid() != Process.CAMERASERVER_UID) {
Slog.e(TAG, "Calling UID: " + Binder.getCallingUid() + " doesn't match expected " +
" camera service UID!");
return;
}
String state = cameraStateToString(newCameraState);
String facingStr = cameraFacingToString(facing);
if (DEBUG) Slog.v(TAG, "Camera " + cameraId + " facing " + facingStr + " state now " +
state + " for client " + clientName + " API Level " + apiLevel);
updateActivityCount(cameraId, newCameraState, facing, clientName, apiLevel);
}
@Override
public boolean captureFrame(String inputId, Surface surface, TvStreamConfig config,
int userId)
throws RemoteException {
ensureCaptureTvInputPermission();
final long identity = Binder.clearCallingIdentity();
final int callingUid = Binder.getCallingUid();
final int resolvedUserId = resolveCallingUserId(Binder.getCallingPid(), callingUid,
userId, "captureFrame");
try {
String hardwareInputId = null;
synchronized (mLock) {
UserState userState = getOrCreateUserStateLocked(resolvedUserId);
if (userState.inputMap.get(inputId) == null) {
Slog.e(TAG, "input not found for " + inputId);
return false;
}
for (SessionState sessionState : userState.sessionStateMap.values()) {
if (sessionState.inputId.equals(inputId)
&& sessionState.hardwareSessionToken != null) {
hardwareInputId = userState.sessionStateMap.get(
sessionState.hardwareSessionToken).inputId;
break;
}
}
}
return mTvInputHardwareManager.captureFrame(
(hardwareInputId != null) ? hardwareInputId : inputId,
surface, config, callingUid, resolvedUserId);
} finally {
Binder.restoreCallingIdentity(identity);
}
}
@VisibleForTesting
protected boolean isCallerEstablishedOwnerLocked() {
return isRunningLocked() && Binder.getCallingUid() == mOwnerUID;
}
private static void enforceSystemUid() {
final int uid = Binder.getCallingUid();
if (uid != Process.SYSTEM_UID) {
throw new SecurityException("Only available to AID_SYSTEM");
}
}
/**
* If the user id supplied is different to the calling user, the caller must hold the
* INTERACT_ACROSS_USERS_FULL permission.
*/
@Override
public void syncAsUser(SyncRequest request, int userId, String callingPackage) {
enforceCrossUserPermission(userId, "no permission to request sync as user: " + userId);
final int callingUid = Binder.getCallingUid();
final int callingPid = Binder.getCallingPid();
final Bundle extras = request.getBundle();
validateExtras(callingUid, extras);
final int syncExemption = getSyncExemptionAndCleanUpExtrasForCaller(callingUid, extras);
// This makes it so that future permission checks will be in the context of this
// process rather than the caller's process. We will restore this before returning.
long identityToken = clearCallingIdentity();
try {
SyncManager syncManager = getSyncManager();
if (syncManager == null) {
return;
}
long flextime = request.getSyncFlexTime();
long runAtTime = request.getSyncRunTime();
if (request.isPeriodic()) {
mContext.enforceCallingOrSelfPermission(
Manifest.permission.WRITE_SYNC_SETTINGS,
"no permission to write the sync settings");
SyncStorageEngine.EndPoint info;
info = new SyncStorageEngine.EndPoint(
request.getAccount(), request.getProvider(), userId);
runAtTime = clampPeriod(runAtTime);
// Schedule periodic sync.
getSyncManager().updateOrAddPeriodicSync(info, runAtTime,
flextime, extras);
} else {
syncManager.scheduleSync(
request.getAccount(), userId, callingUid, request.getProvider(), extras,
SyncStorageEngine.AuthorityInfo.UNDEFINED,
syncExemption, callingUid, callingPid, callingPackage);
}
} finally {
restoreCallingIdentity(identityToken);
}
}
/** {@hide} */
public static final void enforceChangePermission(Context context) {
int uid = Binder.getCallingUid();
Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
.getPackageNameForUid(context, uid), true /* throwException */);
}