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