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.ComponentName; 20 import android.content.SharedPreferences; 21 import android.graphics.drawable.Icon; 22 import android.support.wearable.complications.ComplicationData; 23 import android.support.wearable.complications.ComplicationManager; 24 import android.support.wearable.complications.ComplicationProviderService; 25 import android.support.wearable.complications.ComplicationText; 26 27 /** 28 * A complication provider that supports only {@link ComplicationData#TYPE_RANGED_VALUE} and cycles 29 * through the possible configurations on tap. The value is randomised on each update. 30 */ 31 public class RangedValueProviderService extends ComplicationProviderService { 32 33 private static final float[] MIN_VALUES = {0, -20, 57.5f, 10045}; 34 private static final float[] MAX_VALUES = {100, 20, 824.2f, 100000}; 35 36 @Override 37 public void onComplicationUpdate(int complicationId, int type, ComplicationManager manager) { 38 if (type != ComplicationData.TYPE_RANGED_VALUE) { 39 manager.noUpdateRequired(complicationId); 40 return; 41 } 42 43 ComponentName thisProvider = new ComponentName(this, getClass()); 44 PendingIntent complicationTogglePendingIntent = 45 ComplicationToggleReceiver.getToggleIntent(this, thisProvider, complicationId); 46 47 SharedPreferences preferences = 48 getSharedPreferences(ComplicationToggleReceiver.PREFERENCES_NAME, 0); 49 int state = 50 preferences.getInt( 51 ComplicationToggleReceiver.getPreferenceKey(thisProvider, complicationId), 52 0); 53 54 ComplicationData data = null; 55 56 int caseValue = state % 4; 57 float minValue = MIN_VALUES[caseValue]; 58 float maxValue = MAX_VALUES[caseValue]; 59 float value = (float) Math.random() * (maxValue - minValue) + minValue; 60 61 switch (caseValue) { 62 case 0: 63 data = 64 new ComplicationData.Builder(type) 65 .setMinValue(minValue) 66 .setMaxValue(maxValue) 67 .setValue(value) 68 .setShortText( 69 ComplicationText.plainText( 70 getString(R.string.short_text_only))) 71 .setTapAction(complicationTogglePendingIntent) 72 .build(); 73 break; 74 case 1: 75 data = 76 new ComplicationData.Builder(type) 77 .setMinValue(minValue) 78 .setMaxValue(maxValue) 79 .setValue(value) 80 .setShortText( 81 ComplicationText.plainText( 82 getString(R.string.short_text_with_icon))) 83 .setIcon(Icon.createWithResource(this, R.drawable.ic_battery)) 84 .setBurnInProtectionIcon( 85 Icon.createWithResource( 86 this, R.drawable.ic_battery_burn_protect)) 87 .setTapAction(complicationTogglePendingIntent) 88 .build(); 89 break; 90 case 2: 91 data = 92 new ComplicationData.Builder(type) 93 .setMinValue(minValue) 94 .setMaxValue(maxValue) 95 .setValue(value) 96 .setShortText( 97 ComplicationText.plainText( 98 getString(R.string.short_text_with_title))) 99 .setShortTitle( 100 ComplicationText.plainText(getString(R.string.short_title))) 101 .setTapAction(complicationTogglePendingIntent) 102 .build(); 103 break; 104 case 3: 105 data = 106 new ComplicationData.Builder(type) 107 .setMinValue(minValue) 108 .setMaxValue(maxValue) 109 .setValue(value) 110 .setIcon( 111 Icon.createWithResource( 112 this, R.drawable.ic_event_vd_theme_24)) 113 .setTapAction(complicationTogglePendingIntent) 114 .build(); 115 } 116 manager.updateComplicationData(complicationId, data); 117 } 118 }