Home | History | Annotate | Download | only in tools
      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 
     17 package android.app.cts.android.app.cts.tools;
     18 
     19 import android.app.ActivityManager;
     20 import android.content.Context;
     21 import android.os.SystemClock;
     22 import android.util.Log;
     23 
     24 /**
     25  * Helper for monitoring the importance state of a uid.
     26  */
     27 public final class UidImportanceListener implements ActivityManager.OnUidImportanceListener {
     28     final ActivityManager mAm;
     29     final int mUid;
     30     final int mCutPoint;
     31     final long mDefaultWaitTime;
     32 
     33     int mLastValue = -1;
     34 
     35     public UidImportanceListener(Context context, int uid, int cutPoint) {
     36         this(context, uid, cutPoint, 5*1000);
     37     }
     38 
     39     public UidImportanceListener(Context context, int uid, int cutPoint, long defaultWaitTime) {
     40         mAm = context.getSystemService(ActivityManager.class);
     41         mUid = uid;
     42         mCutPoint = cutPoint;
     43         mDefaultWaitTime = defaultWaitTime;
     44     }
     45 
     46     @Override
     47     public void onUidImportance(int uid, int importance) {
     48         synchronized (this) {
     49             Log.d("XXXXX", "Got importance for uid " + uid + " (cut " + mCutPoint
     50                     + "): " + importance);
     51             if (uid == mUid) {
     52                 mLastValue = importance;
     53                 notifyAll();
     54             }
     55         }
     56     }
     57 
     58     public void register() {
     59         mAm.addOnUidImportanceListener(this, mCutPoint);
     60     }
     61 
     62     public void unregister() {
     63         mAm.removeOnUidImportanceListener(this);
     64     }
     65 
     66     public int waitForValue(int minValue, int maxValue) {
     67         return waitForValue(minValue, maxValue, mDefaultWaitTime);
     68     }
     69 
     70     public int waitForValue(int minValue, int maxValue, long timeout) {
     71         final long endTime = SystemClock.uptimeMillis()+timeout;
     72 
     73         synchronized (this) {
     74             while (mLastValue < minValue || mLastValue > maxValue) {
     75                 final long now = SystemClock.uptimeMillis();
     76                 if (now >= endTime) {
     77                     throw new IllegalStateException("Timed out waiting for importance "
     78                             + minValue + "-" + maxValue + " (cut "
     79                             + mCutPoint + "), last was " + mLastValue);
     80                 }
     81                 try {
     82                     wait(endTime-now);
     83                 } catch (InterruptedException e) {
     84                 }
     85             }
     86             Log.d("XXXX", "waitForValue " + minValue + "-" + maxValue + " (cut "
     87                     + mCutPoint + "): " + mLastValue);
     88             return mLastValue;
     89         }
     90     }
     91 }
     92