1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.crossprofilepermissioncontrol; 18 19 import android.app.Activity; 20 import android.app.AppOpsManager; 21 import android.content.pm.CrossProfileApps; 22 import android.os.Bundle; 23 import android.os.Process; 24 import android.view.View; 25 import android.widget.Button; 26 import android.widget.TextView; 27 28 /** 29 * Test activity to check if the app can interact across profiles. 30 */ 31 public class TestActivity extends Activity { 32 private static final String INTERACT_ACROSS_PROFILES = 33 "android.permission.INTERACT_ACROSS_PROFILES"; 34 private AppOpsManager mAppOpsManager; 35 private CrossProfileApps mCrossProfileApps; 36 private TextView mTextView; 37 private Button mSettingsButton; 38 39 @Override onCreate(Bundle savedInstanceState)40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 setContentView(R.layout.main); 43 mTextView = findViewById(R.id.cross_profile_app_text); 44 mSettingsButton = findViewById(R.id.cross_profile_settings); 45 46 mAppOpsManager = getSystemService(AppOpsManager.class); 47 mCrossProfileApps = getSystemService(CrossProfileApps.class); 48 49 mSettingsButton.setOnClickListener(new View.OnClickListener() { 50 @Override 51 public void onClick(View v) { 52 startActivity(mCrossProfileApps.createRequestInteractAcrossProfilesIntent()); 53 } 54 }); 55 } 56 57 @Override onStart()58 protected void onStart() { 59 super.onStart(); 60 updateText(); 61 } 62 updateText()63 private void updateText() { 64 final String op = AppOpsManager.permissionToOp(INTERACT_ACROSS_PROFILES); 65 if (AppOpsManager.MODE_ALLOWED == mAppOpsManager.checkOpNoThrow( 66 op, Process.myUid(), getPackageName())) { 67 mTextView.setText(R.string.cross_profile_enabled); 68 } else { 69 mTextView.setText(R.string.cross_profile_disabled); 70 } 71 } 72 } 73