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 android.app.notification.legacy.cts; 18 19 import static android.app.NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED; 20 import static java.lang.Thread.sleep; 21 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.util.Log; 26 27 import androidx.test.InstrumentationRegistry; 28 29 public class ZenModeBroadcastReceiver extends BroadcastReceiver { 30 @Override onReceive(Context context, Intent intent)31 public void onReceive(Context context, Intent intent) { 32 if (intent.getAction() == null || 33 intent.getPackage() == null) { 34 return; 35 } 36 37 if (intent.getAction().equals(ACTION_INTERRUPTION_FILTER_CHANGED) && 38 intent.getPackage().equals(InstrumentationRegistry.getContext().getPackageName())) { 39 mCount++; 40 } 41 } 42 reset()43 public void reset() { 44 mCount = 0; 45 mReset = true; 46 } 47 waitFor(int count, int ms)48 public void waitFor(int count, int ms) throws IllegalStateException { 49 if (!mReset) { 50 throw new IllegalStateException("Call reset() before waitFor()!"); 51 } 52 mReset = false; 53 54 final int delayMs = 100; 55 while (ms > 0 && mCount < count) { 56 ms -= delayMs; 57 try { 58 sleep(delayMs); 59 } catch (InterruptedException e) { 60 e.printStackTrace(); 61 } 62 } 63 64 Log.d(TAG, "Exit from wait due to " + 65 (mCount < count ? "timeout" : "intents") + "."); 66 } 67 68 private static String TAG = "CpsTest"; 69 private int mCount = 0; 70 private boolean mReset = true; 71 } 72