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 package com.example.android.wearable.wear.wearcomplicationproviderstestsuite; 17 18 import android.app.PendingIntent; 19 import android.content.BroadcastReceiver; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.SharedPreferences; 24 import android.os.Bundle; 25 import android.support.wearable.complications.ProviderUpdateRequester; 26 27 /** Receives intents on tap and causes complication states to be toggled and updated. */ 28 public class ComplicationToggleReceiver extends BroadcastReceiver { 29 private static final String EXTRA_PROVIDER_COMPONENT = "providerComponent"; 30 private static final String EXTRA_COMPLICATION_ID = "complicationId"; 31 32 static final String PREFERENCES_NAME = "ComplicationTestSuite"; 33 34 @Override 35 public void onReceive(Context context, Intent intent) { 36 Bundle extras = intent.getExtras(); 37 ComponentName provider = extras.getParcelable(EXTRA_PROVIDER_COMPONENT); 38 int complicationId = extras.getInt(EXTRA_COMPLICATION_ID); 39 40 String preferenceKey = getPreferenceKey(provider, complicationId); 41 SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, 0); 42 int value = pref.getInt(preferenceKey, 0); 43 SharedPreferences.Editor editor = pref.edit(); 44 editor.putInt(preferenceKey, value + 1); // Increase value by 1 45 editor.apply(); 46 47 // Request an update for the complication that has just been toggled. 48 ProviderUpdateRequester requester = new ProviderUpdateRequester(context, provider); 49 requester.requestUpdate(complicationId); 50 } 51 52 /** 53 * Returns a pending intent, suitable for use as a tap intent, that causes a complication to be 54 * toggled and updated. 55 */ 56 static PendingIntent getToggleIntent( 57 Context context, ComponentName provider, int complicationId) { 58 Intent intent = new Intent(context, ComplicationToggleReceiver.class); 59 intent.putExtra(EXTRA_PROVIDER_COMPONENT, provider); 60 intent.putExtra(EXTRA_COMPLICATION_ID, complicationId); 61 62 // Pass complicationId as the requestCode to ensure that different complications get 63 // different intents. 64 return PendingIntent.getBroadcast( 65 context, complicationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); 66 } 67 68 /** 69 * Returns the key for the shared preference used to hold the current state of a given 70 * complication. 71 */ 72 static String getPreferenceKey(ComponentName provider, int complicationId) { 73 return provider.getClassName() + complicationId; 74 } 75 }