世事洞明皆学问,人情练达即文章。这篇文章主要讲述Android 7.0 CTA认证蓝牙权限未明示的问题相关的知识,希望能为你提供帮助。
Bluetooth
+++ b/frameworks/base/core/java/com/mediatek/cta/CtaUtils.java @@ -14,6 +14,7 @@ import android.app.AppGlobals; import android.app.AppOpsManager; import android.app.IActivityManager; import android.content.Context; +import android.content.pm.ApplicationInfo; //mh.add bt import android.content.pm.IPackageManager; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; @@ -83,7 +84,7 @@ public class CtaUtils { new String[CtaPermissions.CTA_ADDED_PERMISSION_GROUPS.size()]); } -private static String getCallingPkgName(int pid, int uid) { +public static String getCallingPkgName(int pid, int uid) { IActivityManager am = ActivityManagerNative.getDefault(); List runningProcesses = null; try { @@ -104,6 +105,24 @@ public class CtaUtils { } return null; } + +public static boolean isSystemApp(Context context, String packageName) { +PackageManager pm = context.getPackageManager(); +boolean isSystemApp = false; +int uid = Binder.getCallingUid(); +int userId = UserHandle.getUserId(uid); +try { +ApplicationInfo applicationInfo += pm.getApplicationInfoAsUser(packageName, 0 /* no flags */, userId); +if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 +|| (applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) { +isSystemApp = true; +} +} catch (PackageManager.NameNotFoundException e) { +Slog.d(TAG, "isSystemApp is false for NameNotFoundException= " + e); +} +return isSystemApp; +}
+++ b/frameworks/base/services/core/java/com/android/server/BluetoothManagerService.java @@ -69,6 +69,7 @@ import java.util.NoSuchElementException; import android.os.SystemProperties; /// M: CTA requirement - permission control@{ +import android.content.ActivityNotFoundException; import com.mediatek.cta.CtaUtils; ///@} @@ -817,8 +818,19 @@ class BluetoothManagerService extends IBluetoothManager.Stub { mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission"); /// M: Permission check for CTA requirement @{ -CtaUtils.enforceCheckPermission(com.mediatek.Manifest.permission.CTA_ENABLE_BT, -"Enable bluetooth"); +/*CtaUtils.enforceCheckPermission(com.mediatek.Manifest.permission.CTA_ENABLE_BT, +"Enable bluetooth"); */ +int callingUid = Binder.getCallingUid(); +int callingPid = Binder.getCallingPid(); +final boolean callerSystem = UserHandle.getAppId(callingUid) == Process.SYSTEM_UID; +if (!callerSystem) { +String packageName = CtaUtils.getCallingPkgName(callingPid, callingUid); +if (!isEnabled() & & CtaUtils.isCtaSupported() & & !CtaUtils.isSystemApp(mContext, +packageName) & & startConsentUiIfNeeded(packageName, callingUid, +BluetoothAdapter.ACTION_REQUEST_ENABLE)) { +return false; +} +} ///@} if (DBG) { Slog.d(TAG,"enable():mBluetooth =" + mBluetooth + @@ -834,6 +846,24 @@ class BluetoothManagerService extends IBluetoothManager.Stub { if (DBG) Slog.d(TAG, "enable returning"); return true; } + +/// M: Permission check for CTA requirement +private boolean startConsentUiIfNeeded(String packageName, +int callingUid, String intentAction) { +Intent intent = new Intent(intentAction); +intent.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName); +Slog.e(TAG, "menghua packageName:" + packageName); +intent.setFlags( +Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); +try { +mContext.startActivity(intent); +} catch (ActivityNotFoundException e) { +// Shouldn‘t happen +Slog.e(TAG, "Intent to handle action " + intentAction + " missing"); +return false; +} +return true; +} public boolean disable(boolean persist) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, (END)
+++ b/packages/apps/Settings/src/com/android/settings/bluetooth/RequestPermissionActivity.java @@ -75,6 +75,8 @@ public class RequestPermissionActivity extends Activity implements private boolean mUserConfirmed; private AlertDialog mDialog;
+private String pkgName; //mh.add private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @@ -132,6 +134,7 @@ public class RequestPermissionActivity extends Activity implements intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); if (mEnableOnly) { intent.setAction(RequestPermissionHelperActivity.ACTION_INTERNAL_REQUEST_BT_ON); +intent.putExtra(Intent.EXTRA_PACKAGE_NAME, pkgName); //mh.add } else { intent.setAction(RequestPermissionHelperActivity. ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE); @@ -266,6 +269,8 @@ public class RequestPermissionActivity extends Activity implements private boolean parseIntent() { Intent intent = getIntent(); if (intent != null & & intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_ENABLE)) { +pkgName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME); //mh.add +Log.i(TAG,"menghua pkgName:"+pkgName); mEnableOnly = true; } else if (intent != null & & intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)) {
+++ b/packages/apps/Settings/src/com/android/settings/bluetooth/RequestPermissionHelperActivity.java @@ -27,7 +27,11 @@ import com.android.internal.app.AlertController; import com.android.settings.R; import com.android.settingslib.bluetooth.LocalBluetoothAdapter; import com.android.settingslib.bluetooth.LocalBluetoothManager; - +//mh.add +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.text.TextUtils; +//mh.endprivate boolean mEnableOnly; +private CharSequence appName; //mh.add @Override protected void onCreate(Bundle savedInstanceState) { @@ -73,7 +79,12 @@ public class RequestPermissionHelperActivity extends AlertActivity implements final AlertController.AlertParams p = mAlertParams; if (mEnableOnly) { -p.mMessage = getString(R.string.bluetooth_ask_enablement); +if(appName == null){ +p.mMessage = getString(R.string.bluetooth_ask_enablement); //mh.modify +}else { +p.mMessage = appName + getString(R.string.bluetooth_ask_enablement_joya); //mh.modify +} } else { if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) { p.mMessage = getString(R.string.bluetooth_ask_enablement_and_lasting_discovery); @@ -133,6 +144,23 @@ public class RequestPermissionHelperActivity extends AlertActivity implements private boolean parseIntent() { Intent intent = getIntent(); if (intent != null & & intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON)) { +//mh.add @{ +String pkgName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME); + +if(pkgName != null){ +try { +ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(pkgName, 0); +appName = applicationInfo.loadSafeLabel(getPackageManager()); +} catch (PackageManager.NameNotFoundException e) { +Log.e(TAG, "Couldn‘t find app with package name " + pkgName); +finish(); +return false; +} +} + +Log.i(TAG,"menghua appName:"+appName); +//mh.end @} mEnableOnly = true; } else if (intent != null & & intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE)) {
+++ b/packages/apps/Settings/res/values/strings.xml @@ -268,6 +268,9 @@ < !-- This string asks the user whether or not to allow an app to enable bluetooth. [CHAR LIMIT=100] --> < string name="bluetooth_ask_enablement"> An app wants to turn Bluetooth ON for this device.< /string> + +< !-- mh --> +< string name="bluetooth_ask_enablement_joya"> wants to turn Bluetooth ON for this device.< /string>
【Android 7.0 CTA认证蓝牙权限未明示的问题】Come on, China! Come on, Wuhan!
推荐阅读
- Policy-based Approach(基于策略的方法)
- Ubuntu Redis安装详细步骤图解
- 什么是Web服务()
- Web服务组件
- Web服务的类型
- RESTful Web服务教程
- Web服务的关键术语
- 屏幕刷新率多少合适,图文详细说明电脑屏幕刷新率多少合适
- 笔记本搜索不到无线网,图文详细说明无线网没信号