• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.android.deskclock;
17 
18 import android.app.Activity;
19 import android.app.ListActivity;
20 import android.content.Intent;
21 import android.os.AsyncTask;
22 import android.os.Bundle;
23 import android.os.Parcelable;
24 import android.view.View;
25 import android.widget.Button;
26 import android.widget.ListView;
27 
28 import com.android.deskclock.provider.Alarm;
29 import com.android.deskclock.widget.selector.AlarmSelection;
30 import com.android.deskclock.widget.selector.AlarmSelectionAdapter;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Locale;
35 
36 public class AlarmSelectionActivity extends ListActivity {
37 
38     /** Used by default when an invalid action provided. */
39     private static final int ACTION_INVALID = -1;
40 
41     /** Action used to signify alarm should be dismissed on selection. */
42     public static final int ACTION_DISMISS = 0;
43 
44     public static final String EXTRA_ACTION = "com.android.deskclock.EXTRA_ACTION";
45     public static final String EXTRA_ALARMS = "com.android.deskclock.EXTRA_ALARMS";
46 
47     private final List<AlarmSelection> mSelections = new ArrayList<>();
48 
49     private int mAction;
50 
51     @Override
onCreate(Bundle savedInstanceState)52     protected void onCreate(Bundle savedInstanceState) {
53         // this activity is shown if:
54         // a) no search mode was specified in which case we show all
55         // enabled alarms
56         // b) if search mode was next and there was multiple alarms firing next
57         // (at the same time) then we only show those alarms firing at the same time
58         // c) if search mode was time and there are multiple alarms with that time
59         // then we only show those alarms with that time
60 
61         super.onCreate(savedInstanceState);
62         setContentView(R.layout.selection_layout);
63 
64         final Button cancelButton = (Button) findViewById(R.id.cancel_button);
65         cancelButton.setOnClickListener(new View.OnClickListener() {
66             @Override
67             public void onClick(View v) {
68                 finish();
69             }
70         });
71 
72         final Intent intent = getIntent();
73         final Parcelable[] alarmsFromIntent = intent.getParcelableArrayExtra(EXTRA_ALARMS);
74         mAction = intent.getIntExtra(EXTRA_ACTION, ACTION_INVALID);
75 
76         // reading alarms from intent
77         // PickSelection is started only if there are more than 1 relevant alarm
78         // so no need to check if alarmsFromIntent is empty
79         for (Parcelable parcelable : alarmsFromIntent) {
80             final Alarm alarm = (Alarm) parcelable;
81 
82             // filling mSelections that go into the UI picker list
83             final String label = String.format(Locale.US, "%d %02d", alarm.hour, alarm.minutes);
84             mSelections.add(new AlarmSelection(label, alarm));
85         }
86 
87         setListAdapter(new AlarmSelectionAdapter(this, R.layout.alarm_row, mSelections));
88     }
89 
90     @Override
onListItemClick(ListView l, View v, int position, long id)91     public void onListItemClick(ListView l, View v, int position, long id) {
92         super.onListItemClick(l, v, position, id);
93         // id corresponds to mSelections id because the view adapter used mSelections
94         final AlarmSelection selection = mSelections.get((int) id);
95         final Alarm alarm = selection.getAlarm();
96         if (alarm != null) {
97             new ProcessAlarmActionAsync(alarm, this, mAction).execute();
98         }
99         finish();
100     }
101 
102     private static class ProcessAlarmActionAsync extends AsyncTask<Void, Void, Void> {
103 
104         private final Alarm mAlarm;
105         private final Activity mActivity;
106         private final int mAction;
107 
ProcessAlarmActionAsync(Alarm alarm, Activity activity, int action)108         public ProcessAlarmActionAsync(Alarm alarm, Activity activity, int action) {
109             mAlarm = alarm;
110             mActivity = activity;
111             mAction = action;
112         }
113 
114         @Override
doInBackground(Void... parameters)115         protected Void doInBackground(Void... parameters) {
116             switch (mAction) {
117                 case ACTION_DISMISS:
118                     HandleApiCalls.dismissAlarm(mAlarm, mActivity);
119                     break;
120                 case ACTION_INVALID:
121                     LogUtils.i("Invalid action");
122             }
123             return null;
124         }
125     }
126 }
127