Home | History | Annotate | Download | only in wearcomplicationproviderstestsuite
      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 
     26 /**
     27  * A complication provider that supports only {@link ComplicationData#TYPE_ICON} and cycles through
     28  * a few different icons on each tap.
     29  */
     30 public class IconProviderService extends ComplicationProviderService {
     31 
     32     @Override
     33     public void onComplicationUpdate(int complicationId, int type, ComplicationManager manager) {
     34         if (type != ComplicationData.TYPE_ICON) {
     35             manager.noUpdateRequired(complicationId);
     36             return;
     37         }
     38 
     39         ComponentName thisProvider = new ComponentName(this, getClass());
     40         PendingIntent complicationTogglePendingIntent =
     41                 ComplicationToggleReceiver.getToggleIntent(this, thisProvider, complicationId);
     42 
     43         SharedPreferences preferences =
     44                 getSharedPreferences(ComplicationToggleReceiver.PREFERENCES_NAME, 0);
     45 
     46         int state =
     47                 preferences.getInt(
     48                         ComplicationToggleReceiver.getPreferenceKey(thisProvider, complicationId),
     49                         0);
     50 
     51         ComplicationData data = null;
     52         switch (state % 3) {
     53             case 0:
     54                 data =
     55                         new ComplicationData.Builder(type)
     56                                 .setIcon(
     57                                         Icon.createWithResource(
     58                                                 this, R.drawable.ic_face_vd_theme_24))
     59                                 .setTapAction(complicationTogglePendingIntent)
     60                                 .build();
     61                 break;
     62             case 1:
     63                 // This case includes a burn-in protection icon. If the screen uses burn-in
     64                 // protection, that icon (which avoids solid blocks of color) should be shown in
     65                 // ambient mode.
     66                 data =
     67                         new ComplicationData.Builder(type)
     68                                 .setIcon(Icon.createWithResource(this, R.drawable.ic_battery))
     69                                 .setBurnInProtectionIcon(
     70                                         Icon.createWithResource(
     71                                                 this, R.drawable.ic_battery_burn_protect))
     72                                 .setTapAction(complicationTogglePendingIntent)
     73                                 .build();
     74                 break;
     75             case 2:
     76                 data =
     77                         new ComplicationData.Builder(type)
     78                                 .setIcon(
     79                                         Icon.createWithResource(
     80                                                 this, R.drawable.ic_event_vd_theme_24))
     81                                 .setTapAction(complicationTogglePendingIntent)
     82                                 .build();
     83                 break;
     84         }
     85         manager.updateComplicationData(complicationId, data);
     86     }
     87 }