1 /*
2  * Copyright (C) 2020 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.os.AsyncTask
21 import android.os.Bundle
22 import android.view.View
23 import android.widget.Button
24 import android.widget.ListView
25 
26 import com.android.deskclock.provider.Alarm
27 import com.android.deskclock.widget.selector.AlarmSelection
28 import com.android.deskclock.widget.selector.AlarmSelectionAdapter
29 
30 import java.util.Locale
31 
32 class AlarmSelectionActivity : ListActivity() {
33     private val mSelections: MutableList<AlarmSelection> = ArrayList()
34     private var mAction = 0
35 
onCreatenull36     override fun onCreate(savedInstanceState: Bundle?) {
37         // this activity is shown if:
38         // a) no search mode was specified in which case we show all
39         // enabled alarms
40         // b) if search mode was next and there was multiple alarms firing next
41         // (at the same time) then we only show those alarms firing at the same time
42         // c) if search mode was time and there are multiple alarms with that time
43         // then we only show those alarms with that time
44         super.onCreate(savedInstanceState)
45         setContentView(R.layout.selection_layout)
46 
47         val cancelButton = findViewById<View>(R.id.cancel_button) as Button
48         cancelButton.setOnClickListener { finish() }
49 
50         val intent = intent
51         val alarmsFromIntent = intent.getParcelableArrayExtra(EXTRA_ALARMS)
52         mAction = intent.getIntExtra(EXTRA_ACTION, ACTION_INVALID)
53 
54         // reading alarms from intent
55         // PickSelection is started only if there are more than 1 relevant alarm
56         // so no need to check if alarmsFromIntent is empty
57         for (parcelable in alarmsFromIntent!!) {
58             val alarm = parcelable as Alarm
59 
60             // filling mSelections that go into the UI picker list
61             val label = String.format(Locale.US, "%d %02d", alarm.hour, alarm.minutes)
62             mSelections.add(AlarmSelection(label, alarm))
63         }
64 
65         listAdapter = AlarmSelectionAdapter(this, R.layout.alarm_row, mSelections)
66     }
67 
onListItemClicknull68     public override fun onListItemClick(l: ListView, v: View, position: Int, id: Long) {
69         super.onListItemClick(l, v, position, id)
70         // id corresponds to mSelections id because the view adapter used mSelections
71         val selection = mSelections[id.toInt()]
72         val alarm: Alarm? = selection.alarm
73         alarm?.let {
74             ProcessAlarmActionAsync(it, this, mAction).execute()
75         }
76         finish()
77     }
78 
79     // TODO(b/165664115) Replace deprecated AsyncTask calls
80     private class ProcessAlarmActionAsync(
81         private val mAlarm: Alarm,
82         private val mActivity: Activity,
83         private val mAction: Int
84     ) : AsyncTask<Void?, Void?, Void?>() {
doInBackgroundnull85         override fun doInBackground(vararg parameters: Void?): Void? {
86             when (mAction) {
87                 ACTION_DISMISS -> HandleApiCalls.dismissAlarm(mAlarm, mActivity)
88                 ACTION_INVALID -> LogUtils.i("Invalid action")
89             }
90             return null
91         }
92     }
93 
94     companion object {
95         /** Used by default when an invalid action provided.  */
96         private const val ACTION_INVALID = -1
97 
98         /** Action used to signify alarm should be dismissed on selection.  */
99         const val ACTION_DISMISS = 0
100 
101         const val EXTRA_ACTION = "com.android.deskclock.EXTRA_ACTION"
102         const val EXTRA_ALARMS = "com.android.deskclock.EXTRA_ALARMS"
103     }
104 }