• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.verifier.notifications;
17 
18 
19 import android.app.Activity;
20 import android.app.AutomaticZenRule;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.os.Parcelable;
28 import android.service.notification.Condition;
29 import android.service.notification.ConditionProviderService;
30 import android.util.Log;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 public class MockConditionProvider extends ConditionProviderService {
36     static final String TAG = "MockConditionProvider";
37 
38     static final String PACKAGE_NAME = "com.android.cts.verifier.notifications";
39     static final String PATH = "mock_cp";
40     static final String QUERY = "query_item";
41 
42     static final String SERVICE_BASE = "android.service.notification.cts.MockConditionProvider.";
43     static final String SERVICE_CHECK = SERVICE_BASE + "SERVICE_CHECK";
44     static final String SERVICE_RESET = SERVICE_BASE + "SERVICE_RESET";
45     static final String SERVICE_SUBSCRIBE = SERVICE_BASE + "SERVICE_SUBSCRIBE";
46 
47     static final String EXTRA_PAYLOAD = "PAYLOAD";
48     static final String EXTRA_INT = "INT";
49     static final String EXTRA_BOOLEAN = "BOOLEAN";
50     static final String EXTRA_TAG = "TAG";
51     static final String EXTRA_CODE = "CODE";
52 
53     static final int RESULT_NO_SERVER = Activity.RESULT_FIRST_USER + 1;
54 
55 
56     private ArrayList<Uri> mSubscriptions = new ArrayList<>();
57     private boolean mConnected = false;
58     private BroadcastReceiver mReceiver;
59 
60     @Override
onCreate()61     public void onCreate() {
62         super.onCreate();
63         Log.d(TAG, "created");
64 
65         mSubscriptions = new ArrayList<Uri>();
66 
67         mReceiver = new BroadcastReceiver() {
68             @Override
69             public void onReceive(Context context, Intent intent) {
70                 String action = intent.getAction();
71                 if (SERVICE_CHECK.equals(action)) {
72                     Log.d(TAG, "SERVICE_CHECK");
73                     Bundle bundle = new Bundle();
74                     bundle.putBoolean(EXTRA_BOOLEAN, mConnected);
75                     setResultExtras(bundle);
76                     setResultCode(Activity.RESULT_OK);
77                 } else if (SERVICE_SUBSCRIBE.equals(action)) {
78                     Log.d(TAG, "SERVICE_SUBSCRIBE");
79                     Bundle bundle = new Bundle();
80                     bundle.putParcelableArrayList(EXTRA_PAYLOAD, mSubscriptions);
81                     setResultExtras(bundle);
82                     setResultCode(Activity.RESULT_OK);
83                 } else if (SERVICE_RESET.equals(action)) {
84                     Log.d(TAG, "SERVICE_RESET");
85                     resetData();
86                 } else {
87                     Log.w(TAG, "unknown action");
88                     setResultCode(Activity.RESULT_CANCELED);
89                 }
90             }
91         };
92         IntentFilter filter = new IntentFilter();
93         filter.addAction(SERVICE_CHECK);
94         filter.addAction(SERVICE_SUBSCRIBE);
95         filter.addAction(SERVICE_RESET);
96         registerReceiver(mReceiver, filter);
97     }
98 
99     @Override
onDestroy()100     public void onDestroy() {
101         super.onDestroy();
102         mConnected = false;
103         unregisterReceiver(mReceiver);
104         mReceiver = null;
105         Log.d(TAG, "destroyed");
106     }
107 
resetData()108     public void resetData() {
109         mSubscriptions.clear();
110     }
111 
resetData(Context context)112     public static void resetData(Context context) {
113         sendCommand(context, SERVICE_RESET, null, 0);
114     }
115 
probeConnected(Context context, BooleanResultCatcher catcher)116     public static void probeConnected(Context context, BooleanResultCatcher catcher) {
117         requestConnected(context, SERVICE_CHECK, catcher);
118     }
119 
probeSubscribe(Context context, ParcelableListResultCatcher catcher)120     public static void probeSubscribe(Context context, ParcelableListResultCatcher catcher) {
121         requestParcelableListResult(context, SERVICE_SUBSCRIBE, catcher);
122     }
123 
sendCommand(Context context, String action, String tag, int code)124     private static void sendCommand(Context context, String action, String tag, int code) {
125         Intent broadcast = new Intent(action);
126         if (tag != null) {
127             broadcast.putExtra(EXTRA_TAG, tag);
128             broadcast.putExtra(EXTRA_CODE, code);
129         }
130         context.sendBroadcast(broadcast);
131     }
132 
toConditionId(String queryValue)133     public static Uri toConditionId(String queryValue) {
134         return new Uri.Builder().scheme("scheme")
135                 .appendPath(PATH)
136                 .appendQueryParameter(QUERY, queryValue)
137                 .build();
138     }
139 
140     @Override
onConnected()141     public void onConnected() {
142         Log.d(TAG, "connected");
143         mConnected = true;
144     }
145 
146     @Override
onSubscribe(Uri conditionId)147     public void onSubscribe(Uri conditionId) {
148         Log.d(TAG, "subscribed to " + conditionId);
149         mSubscriptions.add(conditionId);
150     }
151 
152     @Override
onUnsubscribe(Uri conditionId)153     public void onUnsubscribe(Uri conditionId) {
154         Log.d(TAG, "unsubscribed from " + conditionId);
155         mSubscriptions.remove(conditionId);
156     }
157 
158     public abstract static class BooleanResultCatcher extends BroadcastReceiver {
159         @Override
onReceive(Context context, Intent intent)160         public void onReceive(Context context, Intent intent) {
161             accept(getResultExtras(true).getBoolean(EXTRA_BOOLEAN, false));
162         }
163 
accept(boolean result)164         abstract public void accept(boolean result);
165     }
166 
requestConnected(Context context, String action, BooleanResultCatcher catcher)167     private static void requestConnected(Context context, String action,
168             BooleanResultCatcher catcher) {
169         Intent broadcast = new Intent(action);
170         context.sendOrderedBroadcast(broadcast, null, catcher, null, RESULT_NO_SERVER, null, null);
171     }
172 
173     public abstract static class ParcelableListResultCatcher extends BroadcastReceiver {
174         @Override
onReceive(Context context, Intent intent)175         public void onReceive(Context context, Intent intent) {
176             accept(getResultExtras(true).getParcelableArrayList(EXTRA_PAYLOAD));
177         }
178 
accept(List<Parcelable> result)179         abstract public void accept(List<Parcelable> result);
180     }
181 
requestParcelableListResult(Context context, String action, ParcelableListResultCatcher catcher)182     private static void requestParcelableListResult(Context context, String action,
183             ParcelableListResultCatcher catcher) {
184         Intent broadcast = new Intent(action);
185         context.sendOrderedBroadcast(broadcast, null, catcher, null, RESULT_NO_SERVER, null, null);
186     }
187 }
188