1 /*
2  * Copyright (C) 2010 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.usespermissiondiffcertapp;
18 
19 import static android.text.format.DateUtils.SECOND_IN_MILLIS;
20 
21 import android.app.Activity;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.os.MessageQueue.IdleHandler;
27 import android.os.SystemClock;
28 import android.util.Log;
29 
30 public class ReceiveUriActivity extends Activity {
31     static final String TAG = "ReceiveUriActivity";
32     private static final Object sLock = new Object();
33     private static boolean sStarted;
34     private static boolean sNewIntent;
35     private static boolean sDestroyed = true;
36     private static ReceiveUriActivity sCurInstance;
37 
38     private static final long TIMEOUT_MILLIS = 30 * SECOND_IN_MILLIS;
39 
40     Handler mHandler = new Handler();
41 
42     @Override
onCreate(Bundle savedInstanceState)43     protected void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45 
46         synchronized (sLock) {
47             Log.i(TAG, "onCreate: sCurInstance=" + sCurInstance);
48             if (sCurInstance != null) {
49                 finishCurInstance();
50             }
51             sCurInstance = this;
52             sStarted = true;
53             sDestroyed = false;
54             sLock.notifyAll();
55         }
56     }
57 
58     @Override
onNewIntent(Intent intent)59     protected void onNewIntent(Intent intent) {
60         super.onNewIntent(intent);
61 
62         synchronized (sLock) {
63             Log.i(TAG, "onNewIntent: sCurInstance=" + sCurInstance);
64             sNewIntent = true;
65             sLock.notifyAll();
66         }
67     }
68 
69     @Override
onDestroy()70     protected void onDestroy() {
71         super.onDestroy();
72         Log.i(TAG, "onDestroy: sCurInstance=" + sCurInstance);
73         Looper.myQueue().addIdleHandler(new IdleHandler() {
74             @Override
75             public boolean queueIdle() {
76                 synchronized (sLock) {
77                     sDestroyed = true;
78                     sLock.notifyAll();
79                 }
80                 return false;
81             }
82         });
83     }
84 
finishCurInstance()85     public static void finishCurInstance() {
86         synchronized (sLock) {
87             if (sCurInstance != null) {
88                 sCurInstance.finish();
89                 sCurInstance = null;
90             }
91         }
92     }
93 
finishCurInstanceSync()94     public static void finishCurInstanceSync() {
95         finishCurInstance();
96 
97         synchronized (sLock) {
98             final long startTime = SystemClock.uptimeMillis();
99             while (!sDestroyed) {
100                 try {
101                     sLock.wait(TIMEOUT_MILLIS);
102                 } catch (InterruptedException e) {
103                 }
104                 if (SystemClock.uptimeMillis() >= (startTime + TIMEOUT_MILLIS)) {
105                     throw new RuntimeException("Timeout");
106                 }
107             }
108         }
109     }
110 
clearStarted()111     public static void clearStarted() {
112         synchronized (sLock) {
113             sStarted = false;
114         }
115     }
116 
clearNewIntent()117     public static void clearNewIntent() {
118         synchronized (sLock) {
119             sNewIntent = false;
120         }
121     }
122 
waitForStart()123     public static void waitForStart() {
124         synchronized (sLock) {
125             final long startTime = SystemClock.uptimeMillis();
126             while (!sStarted) {
127                 try {
128                     sLock.wait(TIMEOUT_MILLIS);
129                 } catch (InterruptedException e) {
130                 }
131                 if (SystemClock.uptimeMillis() >= (startTime + TIMEOUT_MILLIS)) {
132                     throw new RuntimeException("Timeout");
133                 }
134             }
135         }
136     }
137 
waitForNewIntent()138     public static void waitForNewIntent() {
139         synchronized (sLock) {
140             final long startTime = SystemClock.uptimeMillis();
141             while (!sNewIntent) {
142                 try {
143                     sLock.wait(TIMEOUT_MILLIS);
144                 } catch (InterruptedException e) {
145                 }
146                 if (SystemClock.uptimeMillis() >= (startTime + TIMEOUT_MILLIS)) {
147                     throw new RuntimeException("Timeout");
148                 }
149             }
150         }
151     }
152 }
153