Home | History | Annotate | Download | only in notifications
      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.content.BroadcastReceiver;
     20 import android.content.ComponentName;
     21 import android.net.Uri;
     22 import android.service.notification.ConditionProviderService;
     23 import android.util.Log;
     24 
     25 import java.util.ArrayList;
     26 import java.util.List;
     27 
     28 public class MockConditionProvider extends ConditionProviderService {
     29     static final String TAG = "MockConditionProvider";
     30 
     31     public static final ComponentName COMPONENT_NAME =
     32             new ComponentName("com.android.cts.verifier", MockConditionProvider.class.getName());
     33     static final String PATH = "mock_cp";
     34     static final String QUERY = "query_item";
     35 
     36     private ArrayList<Uri> mSubscriptions = new ArrayList<>();
     37     private boolean mConnected = false;
     38     private BroadcastReceiver mReceiver;
     39     private static MockConditionProvider sConditionProviderInstance = null;
     40 
     41     @Override
     42     public void onCreate() {
     43         super.onCreate();
     44         Log.d(TAG, "created");
     45 
     46         mSubscriptions = new ArrayList<>();
     47     }
     48 
     49     @Override
     50     public void onDestroy() {
     51         super.onDestroy();
     52         mConnected = false;
     53         if (mReceiver != null) {
     54             unregisterReceiver(mReceiver);
     55         }
     56         mReceiver = null;
     57         Log.d(TAG, "destroyed");
     58         sConditionProviderInstance = null;
     59     }
     60 
     61     public boolean isConnected() {
     62         return mConnected;
     63     }
     64 
     65     public static MockConditionProvider getInstance() {
     66         return sConditionProviderInstance;
     67     }
     68 
     69     public void resetData() {
     70         mSubscriptions.clear();
     71     }
     72 
     73     public List<Uri> getSubscriptions() {
     74         return mSubscriptions;
     75     }
     76 
     77     public static Uri toConditionId(String queryValue) {
     78         return new Uri.Builder().scheme("scheme")
     79                 .appendPath(PATH)
     80                 .appendQueryParameter(QUERY, queryValue)
     81                 .build();
     82     }
     83 
     84     @Override
     85     public void onConnected() {
     86         Log.d(TAG, "connected");
     87         mConnected = true;
     88         sConditionProviderInstance = this;
     89     }
     90 
     91     @Override
     92     public void onSubscribe(Uri conditionId) {
     93         Log.d(TAG, "subscribed to " + conditionId);
     94         mSubscriptions.add(conditionId);
     95     }
     96 
     97     @Override
     98     public void onUnsubscribe(Uri conditionId) {
     99         Log.d(TAG, "unsubscribed from " + conditionId);
    100         mSubscriptions.remove(conditionId);
    101     }
    102 }
    103