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 
17 package com.example.android.support.wearable.notifications;
18 
19 import android.app.Activity;
20 import android.app.Notification;
21 import android.app.NotificationManager;
22 import android.app.RemoteInput;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.support.wearable.view.WearableListView;
26 import android.text.TextUtils;
27 import android.view.LayoutInflater;
28 import android.view.ViewGroup;
29 import android.widget.TextView;
30 import android.widget.Toast;
31 
32 public class MainActivity extends Activity implements WearableListView.ClickListener {
33     private static final int SAMPLE_NOTIFICATION_ID = 0;
34     public static final String KEY_REPLY = "reply";
35 
36     @Override
onCreate(Bundle savedInstanceState)37     protected void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39         setContentView(R.layout.activity_main);
40 
41         WearableListView listView = (WearableListView) findViewById(R.id.list);
42         listView.setAdapter(new Adapter(this));
43         listView.setClickListener(this);
44     }
45 
46     @Override
onResume()47     protected void onResume() {
48         super.onResume();
49         if (getIntent() != null) {
50             Bundle inputResults = RemoteInput.getResultsFromIntent(getIntent());
51             if (inputResults != null) {
52                 CharSequence replyText = inputResults.getCharSequence(KEY_REPLY);
53                 if (replyText != null) {
54                     Toast.makeText(this, TextUtils.concat(getString(R.string.reply_was), replyText),
55                             Toast.LENGTH_LONG).show();
56                 }
57             }
58         }
59     }
60 
61     /** Post a new or updated notification using the selected notification options. */
updateNotification(int presetIndex)62     private void updateNotification(int presetIndex) {
63         NotificationPreset preset = NotificationPresets.PRESETS[presetIndex];
64         Notification notif = preset.buildNotification(this);
65         ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
66                 .notify(SAMPLE_NOTIFICATION_ID, notif);
67         finish();
68     }
69 
70     @Override
onClick(WearableListView.ViewHolder v)71     public void onClick(WearableListView.ViewHolder v) {
72         updateNotification((Integer) v.itemView.getTag());
73     }
74 
75     @Override
onTopEmptyRegionClick()76     public void onTopEmptyRegionClick() {
77     }
78 
79     private static final class Adapter extends WearableListView.Adapter {
80         private final Context mContext;
81         private final LayoutInflater mInflater;
82 
Adapter(Context context)83         private Adapter(Context context) {
84             mContext = context;
85             mInflater = LayoutInflater.from(context);
86         }
87 
88         @Override
onCreateViewHolder(ViewGroup parent, int viewType)89         public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
90             return new WearableListView.ViewHolder(
91                     mInflater.inflate(R.layout.notif_preset_list_item, null));
92         }
93 
94         @Override
onBindViewHolder(WearableListView.ViewHolder holder, int position)95         public void onBindViewHolder(WearableListView.ViewHolder holder, int position) {
96             TextView view = (TextView) holder.itemView.findViewById(R.id.name);
97             view.setText(mContext.getString(NotificationPresets.PRESETS[position].nameResId));
98             holder.itemView.setTag(position);
99         }
100 
101         @Override
getItemCount()102         public int getItemCount() {
103             return NotificationPresets.PRESETS.length;
104         }
105     }
106 }
107