1 /*
2  * Copyright (C) 2014 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.watchface;
17 
18 import android.app.Activity;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.res.TypedArray;
23 import android.graphics.drawable.Drawable;
24 import android.os.Bundle;
25 import android.support.wearable.complications.ComplicationHelperActivity;
26 import android.support.wearable.complications.ComplicationProviderInfo;
27 import android.support.wearable.complications.ProviderChooserIntent;
28 import android.support.wearable.view.WearableListView;
29 import android.util.Log;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.ImageView;
34 import android.widget.TextView;
35 
36 import java.util.ArrayList;
37 import java.util.List;
38 
39 /**
40  * The watch-side config activity for {@link ComplicationSimpleWatchFaceService}, which
41  * allows for setting complications on the left and right of watch face.
42  */
43 public class ComplicationSimpleConfigActivity extends Activity implements
44         WearableListView.ClickListener {
45 
46     private static final String TAG = "CompSimpleConfig";
47 
48     private static final int PROVIDER_CHOOSER_REQUEST_CODE = 1;
49 
50     private WearableListView mWearableConfigListView;
51     private ConfigurationAdapter mAdapter;
52 
53     @Override
onCreate(Bundle savedInstanceState)54     protected void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56         setContentView(R.layout.activity_complication_simple_config);
57 
58         mAdapter = new ConfigurationAdapter(getApplicationContext(), getComplicationItems());
59 
60         mWearableConfigListView = (WearableListView) findViewById(R.id.wearable_list);
61         mWearableConfigListView.setAdapter(mAdapter);
62         mWearableConfigListView.setClickListener(this);
63     }
64 
65     @Override
onActivityResult(int requestCode, int resultCode, Intent data)66     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
67         if (requestCode == PROVIDER_CHOOSER_REQUEST_CODE
68                 && resultCode == RESULT_OK) {
69 
70             // Retrieves information for selected Complication provider.
71             ComplicationProviderInfo complicationProviderInfo =
72                     data.getParcelableExtra(ProviderChooserIntent.EXTRA_PROVIDER_INFO);
73 
74             Log.d(TAG, "Selected Provider: " + complicationProviderInfo);
75 
76             finish();
77         }
78     }
79 
80     @Override
onClick(WearableListView.ViewHolder viewHolder)81     public void onClick(WearableListView.ViewHolder viewHolder) {
82         if (Log.isLoggable(TAG, Log.DEBUG)) {
83             Log.d(TAG, "onClick()");
84         }
85 
86         Integer tag = (Integer) viewHolder.itemView.getTag();
87         ComplicationItem complicationItem = mAdapter.getItem(tag);
88 
89         // Note: If you were previously using ProviderChooserIntent.createProviderChooserIntent()
90         // (now deprecated), you will want to switch to
91         // ComplicationHelperActivity.createProviderChooserHelperIntent()
92         startActivityForResult(
93                 ComplicationHelperActivity.createProviderChooserHelperIntent(
94                         getApplicationContext(),
95                         complicationItem.watchFace,
96                         complicationItem.complicationId,
97                         complicationItem.supportedTypes),
98                 PROVIDER_CHOOSER_REQUEST_CODE);
99     }
100 
getComplicationItems()101     private List<ComplicationItem> getComplicationItems() {
102         ComponentName watchFace = new ComponentName(
103                 getApplicationContext(), ComplicationSimpleWatchFaceService.class);
104 
105         String[] complicationNames =
106                 getResources().getStringArray(R.array.complication_simple_names);
107 
108         int[] complicationIds = ComplicationSimpleWatchFaceService.COMPLICATION_IDS;
109 
110         TypedArray icons = getResources().obtainTypedArray(R.array.complication_simple_icons);
111 
112         List<ComplicationItem> items = new ArrayList<>();
113         for (int i = 0; i < complicationIds.length; i++) {
114             items.add(new ComplicationItem(watchFace,
115                     complicationIds[i],
116                     ComplicationSimpleWatchFaceService.COMPLICATION_SUPPORTED_TYPES[i],
117                     icons.getDrawable(i),
118                     complicationNames[i]));
119         }
120         return items;
121     }
122 
123     @Override
onTopEmptyRegionClick()124     public void onTopEmptyRegionClick() {
125         if (Log.isLoggable(TAG, Log.DEBUG)) {
126             Log.d(TAG, "onTopEmptyRegionClick()");
127         }
128     }
129 
130     /*
131      * Inner class representing items of the ConfigurationAdapter (WearableListView.Adapter) class.
132      */
133     private final class ComplicationItem {
134         ComponentName watchFace;
135         int complicationId;
136         int[] supportedTypes;
137         Drawable icon;
138         String title;
139 
ComplicationItem(ComponentName watchFace, int complicationId, int[] supportedTypes, Drawable icon, String title)140         public ComplicationItem(ComponentName watchFace, int complicationId, int[] supportedTypes,
141                                 Drawable icon, String title) {
142             this.watchFace = watchFace;
143             this.complicationId = complicationId;
144             this.supportedTypes = supportedTypes;
145             this.icon = icon;
146             this.title = title;
147         }
148     }
149 
150     private static class ConfigurationAdapter extends WearableListView.Adapter {
151 
152         private Context mContext;
153         private final LayoutInflater mInflater;
154         private List<ComplicationItem> mItems;
155 
156 
ConfigurationAdapter(Context context, List<ComplicationItem> items)157         public ConfigurationAdapter (Context context, List<ComplicationItem> items) {
158             mContext = context;
159             mInflater = LayoutInflater.from(mContext);
160             mItems = items;
161         }
162 
163         // Provides a reference to the type of views you're using
164         public static class ItemViewHolder extends WearableListView.ViewHolder {
165             private ImageView iconImageView;
166             private TextView textView;
ItemViewHolder(View itemView)167             public ItemViewHolder(View itemView) {
168                 super(itemView);
169                 iconImageView = (ImageView) itemView.findViewById(R.id.icon);
170                 textView = (TextView) itemView.findViewById(R.id.name);
171             }
172         }
173 
174         @Override
onCreateViewHolder(ViewGroup parent, int viewType)175         public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
176 
177             // Inflate custom layout for list items.
178             return new ItemViewHolder(
179                     mInflater.inflate(R.layout.activity_complication_simple_list_item, null));
180         }
181 
182         @Override
onBindViewHolder(WearableListView.ViewHolder holder, int position)183         public void onBindViewHolder(WearableListView.ViewHolder holder, int position) {
184 
185             ItemViewHolder itemHolder = (ItemViewHolder) holder;
186 
187             ImageView imageView = itemHolder.iconImageView;
188             imageView.setImageDrawable(mItems.get(position).icon);
189 
190             TextView textView = itemHolder.textView;
191             textView.setText(mItems.get(position).title);
192 
193             holder.itemView.setTag(position);
194         }
195 
196         @Override
getItemCount()197         public int getItemCount() {
198             return mItems.size();
199         }
200 
getItem(int position)201         public ComplicationItem getItem(int position) {
202             return mItems.get(position);
203         }
204     }
205 }