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_LARGE_IMAGE} and cycles
     28  * between a couple of images on tap.
     29  */
     30 public class LargeImageProviderService extends ComplicationProviderService {
     31 
     32     @Override
     33     public void onComplicationUpdate(int complicationId, int type, ComplicationManager manager) {
     34         if (type != ComplicationData.TYPE_LARGE_IMAGE) {
     35             manager.noUpdateRequired(complicationId);
     36             return;
     37         }
     38 
     39         ComponentName thisProvider = new ComponentName(this, getClass());
     40 
     41         // On many watch faces a large image complication might not respond to taps as the
     42         // complication is used to provide the background for the watch. Providers should not rely
     43         // on tap functionality for large image complications, but the tap action is still included
     44         // here in case it is supported.
     45         PendingIntent complicationTogglePendingIntent =
     46                 ComplicationToggleReceiver.getToggleIntent(this, thisProvider, complicationId);
     47 
     48         SharedPreferences preferences =
     49                 getSharedPreferences(ComplicationToggleReceiver.PREFERENCES_NAME, 0);
     50         int state =
     51                 preferences.getInt(
     52                         ComplicationToggleReceiver.getPreferenceKey(thisProvider, complicationId),
     53                         0);
     54 
     55         ComplicationData data = null;
     56         switch (state % 2) {
     57             case 0:
     58                 data =
     59                         new ComplicationData.Builder(type)
     60                                 .setLargeImage(Icon.createWithResource(this, R.drawable.aquarium))
     61                                 .setTapAction(complicationTogglePendingIntent)
     62                                 .build();
     63                 break;
     64             case 1:
     65                 data =
     66                         new ComplicationData.Builder(type)
     67                                 .setLargeImage(Icon.createWithResource(this, R.drawable.outdoors))
     68                                 .setTapAction(complicationTogglePendingIntent)
     69                                 .build();
     70                 break;
     71         }
     72         manager.updateComplicationData(complicationId, data);
     73     }
     74 }