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 android.app.Service;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.Handler;
24 import android.os.IBinder;
25 import android.os.Looper;
26 import android.os.SystemClock;
27 import android.os.MessageQueue.IdleHandler;
28 
29 public class ReceiveUriService extends Service {
30     private static final Object sLock = new Object();
31     private static boolean sStarted;
32     private static boolean sDestroyed;
33     private static int sCurStartId;
34     private static ReceiveUriService sCurInstance;
35 
36     Handler mHandler = new Handler();
37 
38     @Override
onStartCommand(Intent intent, int flags, int startId)39     public int onStartCommand(Intent intent, int flags, int startId) {
40         synchronized (sLock) {
41             sCurStartId = startId;
42             sCurInstance = this;
43             sStarted = true;
44             sDestroyed = false;
45             sLock.notifyAll();
46         }
47 
48         return START_REDELIVER_INTENT;
49     }
50 
51     @Override
onDestroy()52     public void onDestroy() {
53         super.onDestroy();
54         Looper.myQueue().addIdleHandler(new IdleHandler() {
55             @Override
56             public boolean queueIdle() {
57                 synchronized (sLock) {
58                     sDestroyed = true;
59                     sCurInstance = null;
60                     sLock.notifyAll();
61                 }
62                 return false;
63             }
64         });
65     }
66 
67     @Override
onBind(Intent intent)68     public IBinder onBind(Intent intent) {
69         return null;
70     }
71 
stop(Context context)72     public static void stop(Context context) {
73         Intent intent = new Intent();
74         intent.setComponent(new ComponentName(
75                 "com.android.cts.usespermissiondiffcertapp",
76                 "com.android.cts.usespermissiondiffcertapp.ReceiveUriService"));
77         context.stopService(intent);
78     }
79 
getCurStartId()80     public static int getCurStartId() {
81         synchronized (sLock) {
82             return sCurStartId;
83         }
84     }
85 
stopCurWithId(int id)86     public static void stopCurWithId(int id) {
87         synchronized (sLock) {
88             sCurInstance.stopSelf(id);
89         }
90     }
91 
stopSync(Context context)92     public static void stopSync(Context context) {
93         stop(context);
94 
95         synchronized (sLock) {
96             final long startTime = SystemClock.uptimeMillis();
97             while (!sDestroyed) {
98                 try {
99                     sLock.wait(5000);
100                 } catch (InterruptedException e) {
101                 }
102                 if (SystemClock.uptimeMillis() >= (startTime+5000)) {
103                     throw new RuntimeException("Timeout");
104                 }
105             }
106         }
107     }
108 
clearStarted()109     public static void clearStarted() {
110         synchronized (sLock) {
111             sStarted = false;
112         }
113     }
114 
waitForStart()115     public static void waitForStart() {
116         synchronized (sLock) {
117             final long startTime = SystemClock.uptimeMillis();
118             while (!sStarted) {
119                 try {
120                     sLock.wait(5000);
121                 } catch (InterruptedException e) {
122                 }
123                 if (SystemClock.uptimeMillis() >= (startTime+5000)) {
124                     throw new RuntimeException("Timeout");
125                 }
126             }
127         }
128     }
129 }
130