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