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