1 /* 2 * Copyright (C) 2017 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 package com.android.cts.launchertests.support; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.os.UserHandle; 22 import android.os.UserManager; 23 import android.util.Log; 24 25 /** 26 * Runs {@link UserManager#requestQuietModeEnabled(boolean, UserHandle)} APIs by receiving 27 * broadcast and returns the result back to the broadcast sender. 28 */ 29 public class QuietModeCommandReceiver extends BroadcastReceiver { 30 private static final String TAG = "QuietModeReceiver"; 31 private static final String ACTION_TOGGLE_QUIET_MODE = "toggle_quiet_mode"; 32 private static final String EXTRA_QUIET_MODE = "quiet_mode"; 33 private static final String RESULT_SECURITY_EXCEPTION = "security-exception"; 34 35 @Override onReceive(Context context, Intent intent)36 public void onReceive(Context context, Intent intent) { 37 if (!ACTION_TOGGLE_QUIET_MODE.equals(intent.getAction())) { 38 return; 39 } 40 final UserManager userManager = context.getSystemService(UserManager.class); 41 final boolean enableQuietMode = intent.getBooleanExtra(EXTRA_QUIET_MODE, false); 42 final UserHandle targetUser = intent.getParcelableExtra(Intent.EXTRA_USER); 43 String result; 44 try { 45 final boolean setQuietModeResult = 46 userManager.requestQuietModeEnabled(enableQuietMode, targetUser); 47 result = Boolean.toString(setQuietModeResult); 48 Log.i(TAG, "trySetQuietModeEnabled returns " + setQuietModeResult); 49 } catch (SecurityException ex) { 50 Log.i(TAG, "trySetQuietModeEnabled throws security exception", ex); 51 result = RESULT_SECURITY_EXCEPTION; 52 } 53 setResultData(result); 54 } 55 } 56