OSDN Git Service

Extend the ListView selection behind the checkbox.
authorPatrick Scott <phanna@android.com>
Tue, 14 Apr 2009 20:55:10 +0000 (16:55 -0400)
committerPatrick Scott <phanna@android.com>
Wed, 15 Apr 2009 17:04:46 +0000 (13:04 -0400)
Extend the selection behind the checkbox and change the behavior of selecting an
alarm item. Long pressing the item now brings up a context menu that can enable
or disable the alarm in addition to deleting the alarm.

The context menu header now contains the label of the alarm if it exists and
it mimics the standard dialog header view.

Updated some of the layout code to ensure that the default three Alarms do not
show a scrollbar.

BUG=1438269

res/layout/alarm_time.xml
res/layout/context_menu_header.xml [new file with mode: 0644]
res/menu/context_menu.xml [new file with mode: 0644]
res/values/strings.xml
src/com/android/alarmclock/AlarmClock.java

index c2bc65e..620a620 100644 (file)
     <com.android.alarmclock.DigitalClock android:id="@+id/digitalClock"
         android:layout_width="wrap_content"
         android:layout_height="fill_parent"
-        android:focusable="true"
         android:layout_weight="1"
         android:gravity="center_vertical"
         android:orientation="vertical"
         android:paddingLeft="8dip"
-        android:paddingRight="8dip"
-        android:background="@android:drawable/menuitem_background">
+        android:paddingRight="8dip">
 
         <LinearLayout
             android:layout_width="fill_parent"
@@ -88,6 +86,7 @@
     </com.android.alarmclock.DigitalClock>
 
     <CheckBox android:id="@+id/alarmButton"
+        android:focusable="false"
         android:layout_width="60dip"
         android:layout_height="76dip"
         android:layout_gravity="center_vertical"/>
diff --git a/res/layout/context_menu_header.xml b/res/layout/context_menu_header.xml
new file mode 100644 (file)
index 0000000..6ac4dc5
--- /dev/null
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2009 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+  
+          http://www.apache.org/licenses/LICENSE-2.0
+  
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="fill_parent"
+    android:layout_height="wrap_content"
+    android:paddingTop="6dip"
+    android:paddingBottom="9dip"
+    android:paddingLeft="10dip"
+    android:paddingRight="10dip">
+
+    <ImageView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="top"
+        android:paddingTop="6dip"
+        android:paddingRight="10dip"
+        android:src="@*android:drawable/ic_dialog_time"/>
+
+    <TextView android:id="@+id/header_time"
+        style="?android:attr/textAppearanceLarge"
+        android:layout_width="wrap_content"
+        android:layout_height="fill_parent"
+        android:singleLine="true"
+        android:gravity="center_vertical"
+        android:ellipsize="none"/>
+
+    <TextView android:id="@+id/header_label"
+        style="?android:attr/textAppearanceLarge"
+        android:layout_width="fill_parent"
+        android:layout_height="fill_parent"
+        android:layout_marginLeft="20dip"
+        android:singleLine="true"
+        android:gravity="right|center_vertical"
+        android:ellipsize="end"/>
+
+</LinearLayout>
+
diff --git a/res/menu/context_menu.xml b/res/menu/context_menu.xml
new file mode 100644 (file)
index 0000000..25a62be
--- /dev/null
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2009 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:id="@+id/enable_alarm"
+        android:title="@string/enable_alarm"/>
+    <item android:id="@+id/delete_alarm"
+        android:title="@string/delete_alarm"/>
+</menu>
+
+
index e84f68a..51717ac 100644 (file)
     <!-- Context Menu Item on Alarm Settings screen: Delete alarm -->
     <string name="delete_alarm">Delete alarm</string>
 
+    <!-- Context Menu Item on Alarm Settings screen: Enable alarm -->
+    <string name="enable_alarm">Enable alarm</string>
+
+    <!-- Context Menu Item on Alarm Settings screen: Disable alarm -->
+    <string name="disable_alarm">Disable alarm</string>
+
     <!-- Delete alarm confirmation dialog message. -->
     <string name="delete_alarm_confirm">This alarm will be deleted.</string>
 
index 2b542b6..c24f20a 100644 (file)
@@ -34,7 +34,11 @@ import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.View.OnClickListener;
+import android.view.View.OnCreateContextMenuListener;
 import android.view.ViewGroup;
+import android.widget.AdapterView;
+import android.widget.AdapterView.AdapterContextMenuInfo;
+import android.widget.AdapterView.OnItemClickListener;
 import android.widget.CursorAdapter;
 import android.widget.ListView;
 import android.widget.TextView;
@@ -45,7 +49,7 @@ import java.util.Calendar;
 /**
  * AlarmClock application.
  */
-public class AlarmClock extends Activity {
+public class AlarmClock extends Activity implements OnItemClickListener {
 
     final static String PREFERENCES = "AlarmClock";
     final static String PREF_CLOCK_FACE = "face";
@@ -62,8 +66,6 @@ public class AlarmClock extends Activity {
     private LayoutInflater mFactory;
     private ViewGroup mClockLayout;
     private View mClock = null;
-    private MenuItem mAddAlarmItem;
-    private MenuItem mToggleClockItem;
     private ListView mAlarmsList;
     private Cursor mCursor;
 
@@ -123,22 +125,6 @@ public class AlarmClock extends Activity {
             if (Log.LOGV) Log.v("bindView " + cursor.getPosition() + " " + id + " " + hour +
                                 ":" + minutes + " " + daysOfWeek.toString(context, true) + " dc " + digitalClock);
 
-            digitalClock.setOnClickListener(new OnClickListener() {
-                    public void onClick(View v) {
-                        if (true) {
-                            Intent intent = new Intent(AlarmClock.this, SetAlarm.class);
-                            intent.putExtra(Alarms.ID, id);
-                            startActivity(intent);
-                        } else {
-                            // TESTING: immediately pop alarm
-                            Intent fireAlarm = new Intent(AlarmClock.this, AlarmAlert.class);
-                            fireAlarm.putExtra(Alarms.ID, id);
-                            fireAlarm.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-                            startActivity(fireAlarm);
-                        }
-                    }
-                });
-
             // set the alarm text
             final Calendar c = Calendar.getInstance();
             c.set(Calendar.HOUR_OF_DAY, hour);
@@ -165,34 +151,55 @@ public class AlarmClock extends Activity {
             } else {
                 labelView.setVisibility(View.GONE);
             }
-
-            // Build context menu
-            digitalClock.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
-                    public void onCreateContextMenu(ContextMenu menu, View view,
-                                                    ContextMenuInfo menuInfo) {
-                        menu.setHeaderTitle(Alarms.formatTime(AlarmClock.this, c));
-                        MenuItem deleteAlarmItem = menu.add(0, id, 0, R.string.delete_alarm);
-                    }
-                });
         }
     };
 
+    private boolean isAlarmEnabled(final Cursor c) {
+        return c.getInt(Alarms.AlarmColumns.ALARM_ENABLED_INDEX) == 1;
+    }
+
     @Override
     public boolean onContextItemSelected(final MenuItem item) {
-        // Confirm that the alarm will be deleted.
-        new AlertDialog.Builder(this)
-                .setTitle(getString(R.string.delete_alarm))
-                .setMessage(getString(R.string.delete_alarm_confirm))
-                .setPositiveButton(android.R.string.ok,
-                        new DialogInterface.OnClickListener() {
-                            public void onClick(DialogInterface d, int w) {
-                                Alarms.deleteAlarm(AlarmClock.this,
-                                        item.getItemId());
-                            }
-                        })
-                .setNegativeButton(android.R.string.cancel, null)
-                .show();
-        return true;
+        final AdapterContextMenuInfo info =
+                (AdapterContextMenuInfo) item.getMenuInfo();
+        final int id = (int) info.id;
+        switch (item.getItemId()) {
+            case R.id.delete_alarm:
+                // Confirm that the alarm will be deleted.
+                new AlertDialog.Builder(this)
+                        .setTitle(getString(R.string.delete_alarm))
+                        .setMessage(getString(R.string.delete_alarm_confirm))
+                        .setPositiveButton(android.R.string.ok,
+                                new DialogInterface.OnClickListener() {
+                                    public void onClick(DialogInterface d,
+                                            int w) {
+                                        Alarms.deleteAlarm(AlarmClock.this, id);
+                                    }
+                                })
+                        .setNegativeButton(android.R.string.cancel, null)
+                        .show();
+                return true;
+
+            case R.id.enable_alarm:
+                final Cursor c = (Cursor) mAlarmsList.getAdapter()
+                        .getItem(info.position);
+                boolean enabled = isAlarmEnabled(c);
+                Alarms.enableAlarm(this, id, !enabled);
+                if (!enabled) {
+                    final int hour =
+                            c.getInt(Alarms.AlarmColumns.ALARM_HOUR_INDEX);
+                    final int minutes =
+                            c.getInt(Alarms.AlarmColumns.ALARM_MINUTES_INDEX);
+                    final Alarms.DaysOfWeek daysOfWeek = new Alarms.DaysOfWeek(
+                            c.getInt(Alarms.AlarmColumns.ALARM_DAYS_OF_WEEK_INDEX));
+                    SetAlarm.popAlarmSetToast(this, hour, minutes, daysOfWeek);
+                }
+                return true;
+
+            default:
+                break;
+        }
+        return super.onContextItemSelected(item);
     }
 
     @Override
@@ -229,7 +236,8 @@ public class AlarmClock extends Activity {
         mAlarmsList = (ListView) findViewById(R.id.alarms_list);
         mAlarmsList.setAdapter(new AlarmTimeAdapter(this, mCursor));
         mAlarmsList.setVerticalScrollBarEnabled(true);
-        mAlarmsList.setItemsCanFocus(true);
+        mAlarmsList.setOnItemClickListener(this);
+        mAlarmsList.setOnCreateContextMenuListener(this);
 
         mClockLayout = (ViewGroup) findViewById(R.id.clock_layout);
         mClockLayout.setOnClickListener(new View.OnClickListener() {
@@ -280,6 +288,48 @@ public class AlarmClock extends Activity {
         return super.onCreateOptionsMenu(menu);
     }
 
+    @Override
+    public void onCreateContextMenu(ContextMenu menu, View view,
+            ContextMenuInfo menuInfo) {
+        // Inflate the menu from xml.
+        getMenuInflater().inflate(R.menu.context_menu, menu);
+
+        // Use the current item to create a custom view for the header.
+        final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
+        final Cursor c =
+                (Cursor) mAlarmsList.getAdapter().getItem((int) info.position);
+        final int hour = c.getInt(Alarms.AlarmColumns.ALARM_HOUR_INDEX);
+        final int minutes = c.getInt(Alarms.AlarmColumns.ALARM_MINUTES_INDEX);
+        final String label =
+                c.getString(Alarms.AlarmColumns.ALARM_MESSAGE_INDEX);
+
+        // Construct the Calendar to compute the time.
+        final Calendar cal = Calendar.getInstance();
+        cal.set(Calendar.HOUR_OF_DAY, hour);
+        cal.set(Calendar.MINUTE, minutes);
+        final String time = Alarms.formatTime(this, cal);
+
+        // Inflate the custom view and set each TextView's text.
+        final View v = mFactory.inflate(R.layout.context_menu_header, null);
+        TextView textView = (TextView) v.findViewById(R.id.header_time);
+        textView.setText(time);
+        textView = (TextView) v.findViewById(R.id.header_label);
+        textView.setText(label);
+
+        // Set the custom view on the menu.
+        menu.setHeaderView(v);
+        // Change the text to "disable" if the alarm is already enabled.
+        if (isAlarmEnabled(c)) {
+            menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm);
+        }
+    }
+
+    public void onItemClick(AdapterView parent, View v, int pos, long id) {
+        Intent intent = new Intent(this, SetAlarm.class);
+        intent.putExtra(Alarms.ID, (int) id);
+        startActivity(intent);
+    }
+
     /**
      * Only allow user to add a new alarm if there are fewer than
      * MAX_ALARM_COUNT