OSDN Git Service

auto import from //depot/cupcake/@136594
authorThe Android Open Source Project <initial-contribution@android.com>
Thu, 5 Mar 2009 22:34:37 +0000 (14:34 -0800)
committerThe Android Open Source Project <initial-contribution@android.com>
Thu, 5 Mar 2009 22:34:37 +0000 (14:34 -0800)
res/layout/alarm_clock.xml
res/layout/alarm_time.xml
res/values/strings.xml
res/xml/alarm_prefs.xml
src/com/android/alarmclock/AlarmAlert.java
src/com/android/alarmclock/AlarmClock.java
src/com/android/alarmclock/AlarmReceiver.java
src/com/android/alarmclock/Alarms.java
src/com/android/alarmclock/SetAlarm.java

index a0ca8e3..b56067d 100644 (file)
     <LinearLayout
         android:id="@+id/clock_layout"
         android:layout_width="fill_parent"
-        android:layout_height="208dip"
+        android:layout_height="wrap_content"
         android:layout_marginBottom="8dip"
         android:gravity="center"/> 
 
     <ListView
         android:id="@+id/alarms_list"
         android:layout_width="fill_parent"
-        android:layout_height="0dip"
-        android:layout_weight="1.0" />
+        android:layout_height="wrap_content"
+        android:layout_weight="1" />
 
 </LinearLayout>
index aae6ef2..3a50070 100644 (file)
@@ -16,7 +16,7 @@
 
 <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="fill_parent" 
+    android:layout_width="fill_parent"
     android:layout_height="wrap_content">
 
     <!-- A layout that displays the time.  Shows time, am/pm (if 12-hour),
         android:layout_height="fill_parent"
         android:focusable="true"
         android:layout_weight="1"
-        android:layout_gravity="center_vertical"
+        android:gravity="center_vertical"
         android:orientation="vertical"
-        android:paddingTop="4dp"
         android:paddingLeft="8dp"
+        android:paddingRight="8dp"
         android:background="@android:drawable/menuitem_background">
 
         <LinearLayout
             android:layout_width="fill_parent"
-            android:layout_height="wrap_content"
-            android:gravity="left">
+            android:layout_height="wrap_content">
 
             <TextView android:id="@+id/timeDisplay"
                 android:layout_width="wrap_content"
@@ -44,7 +43,6 @@
                 android:paddingTop="-4dp"
                 android:paddingBottom="-4dp"
                 android:textSize="28sp"
-                android:gravity="left"
                 android:textColor="@color/white"/>
 
             <LinearLayout android:id="@+id/am_pm"
                     android:text="@string/pm"
                     android:textSize="12sp"/>
             </LinearLayout>
+
+            <TextView android:id="@+id/label"
+                android:layout_width="fill_parent"
+                android:layout_height="wrap_content"
+                android:text="@string/default_label"
+                android:paddingTop="-4dp"
+                android:paddingBottom="-4dp"
+                android:textSize="28sp"
+                android:textColor="@color/white"
+                android:layout_marginLeft="20dp"
+                android:singleLine="true"/>
+
         </LinearLayout>
 
         <TextView android:id="@+id/daysOfWeek"
@@ -74,7 +84,7 @@
             android:textSize="10sp"
             android:textColor="@color/ltgrey"
             android:layout_marginLeft="2dp"
-            android:gravity="left"/>
+            android:visibility="gone"/>
 
     </com.android.alarmclock.DigitalClock>
 
index 30b8fe2..d5d6760 100644 (file)
     <!-- Menu item on Alarm Clock screen: Hide clock -->
     <string name="hide_clock">Hide clock</string>
 
+    <!-- Setting label on Set alarm screen: Label -->
+    <string name="label">Label</string>
+
+    <!-- Default label to display for an alarm -->
+    <string name="default_label">Alarm</string>
+
     <!-- Preference category on Alarm Settings screen: Set alarm -->
     <string name="set_alarm">Set alarm</string>
 
index bdcf6b7..dfb0401 100644 (file)
@@ -33,4 +33,7 @@
         android:title="@string/alarm_repeat"
         android:entries="@array/days_of_week"
         android:entryValues="@array/days_of_week"/>
+    <EditTextPreference android:key="label"
+        android:title="@string/label"
+        android:dialogTitle="@string/label" />
 </PreferenceScreen>
index eefd5bb..2464c96 100644 (file)
@@ -54,6 +54,7 @@ public class AlarmAlert extends Activity {
 
     private AlarmKlaxon mKlaxon;
     private int mAlarmId;
+    private String mLabel;
 
     @Override
     protected void onCreate(Bundle icicle) {
@@ -81,7 +82,11 @@ public class AlarmAlert extends Activity {
 
         mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
 
-        mAlarmId = getIntent().getIntExtra(Alarms.ID, -1);
+        Intent i = getIntent();
+        mAlarmId = i.getIntExtra(Alarms.ID, -1);
+
+        /* Set the title from the passed in label */
+        setTitleFromIntent(i);
 
         /* allow next alarm to trigger while this activity is
            active */
@@ -108,6 +113,14 @@ public class AlarmAlert extends Activity {
         updateLayout();
     }
 
+    private void setTitleFromIntent(Intent i) {
+        mLabel = i.getStringExtra(Alarms.LABEL);
+        if (mLabel == null || mLabel.length() == 0) {
+            mLabel = getString(R.string.default_label);
+        }
+        setTitle(mLabel);
+    }
+
     private void updateSilencedText() {
         TextView silenced = (TextView) findViewById(R.id.silencedText);
         silenced.setText(getString(R.string.alarm_alert_alert_silenced,
@@ -181,7 +194,8 @@ public class AlarmAlert extends Activity {
                     Alarms.formatTime(AlarmAlert.this, c));
             mState = DISMISS;
         } else {
-            Alarms.saveSnoozeAlert(AlarmAlert.this, mAlarmId, snoozeTime);
+            Alarms.saveSnoozeAlert(AlarmAlert.this, mAlarmId, snoozeTime,
+                    mLabel);
             Alarms.setNextAlert(AlarmAlert.this);
             displayTime = getString(R.string.alarm_alert_snooze_set,
                     SNOOZE_MINUTES);
@@ -218,6 +232,7 @@ public class AlarmAlert extends Activity {
         disableKeyguard();
 
         mAlarmId = intent.getIntExtra(Alarms.ID, -1);
+        setTitleFromIntent(intent);
 
         /* unset silenced message */
         TextView silenced = (TextView)findViewById(R.id.silencedText);
index 6230d16..ca1c900 100644 (file)
@@ -104,6 +104,8 @@ public class AlarmClock extends Activity {
             final Alarms.DaysOfWeek daysOfWeek = new Alarms.DaysOfWeek(
                     cursor.getInt(Alarms.AlarmColumns.ALARM_DAYS_OF_WEEK_INDEX));
             final boolean enabled = cursor.getInt(Alarms.AlarmColumns.ALARM_ENABLED_INDEX) == 1;
+            final String label =
+                    cursor.getString(Alarms.AlarmColumns.ALARM_MESSAGE_INDEX);
 
             CheckBox onButton = (CheckBox)view.findViewById(R.id.alarmButton);
             onButton.setChecked(enabled);
@@ -143,8 +145,26 @@ public class AlarmClock extends Activity {
             c.set(Calendar.HOUR_OF_DAY, hour);
             c.set(Calendar.MINUTE, minutes);
             digitalClock.updateTime(c);
+
+            // Set the repeat text or leave it blank if it does not repeat.
             TextView daysOfWeekView = (TextView) digitalClock.findViewById(R.id.daysOfWeek);
-            daysOfWeekView.setText(daysOfWeek.toString(AlarmClock.this, false));
+            final String daysOfWeekStr =
+                    daysOfWeek.toString(AlarmClock.this, false);
+            if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) {
+                daysOfWeekView.setText(daysOfWeekStr);
+                daysOfWeekView.setVisibility(View.VISIBLE);
+            } else {
+                daysOfWeekView.setVisibility(View.GONE);
+            }
+
+            // Display the label
+            TextView labelView =
+                    (TextView) digitalClock.findViewById(R.id.label);
+            if (label != null && label.length() != 0) {
+                labelView.setText(label);
+            } else {
+                labelView.setText(R.string.default_label);
+            }
 
             // Build context menu
             digitalClock.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
index af18b7b..1137952 100644 (file)
@@ -62,6 +62,7 @@ public class AlarmReceiver extends BroadcastReceiver {
          * so that the current app's notification management is not disturbed */
         Intent fireAlarm = new Intent(context, AlarmAlert.class);
         fireAlarm.putExtra(Alarms.ID, id);
+        fireAlarm.putExtra(Alarms.LABEL, intent.getStringExtra(Alarms.LABEL));
         fireAlarm.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
         context.startActivity(fireAlarm);
    }
index 56af9b3..8817234 100644 (file)
@@ -42,9 +42,11 @@ public class Alarms {
     public final static String ALARM_ALERT_ACTION = "com.android.alarmclock.ALARM_ALERT";
     public final static String ID = "alarm_id";
     public final static String TIME = "alarm_time";
+    public final static String LABEL = "alarm_label";
 
     final static String PREF_SNOOZE_ID = "snooze_id";
     final static String PREF_SNOOZE_TIME = "snooze_time";
+    final static String PREF_SNOOZE_LABEL = "snooze_label";
 
     private final static String DM12 = "E h:mm aa";
     private final static String DM24 = "E k:mm";
@@ -480,8 +482,9 @@ public class Alarms {
      * Calculates next scheduled alert
      */
     static class AlarmCalculator implements AlarmSettings {
-        public long mMinAlert = Long.MAX_VALUE;
-        public int mMinIdx = -1;
+        private long mMinAlert = Long.MAX_VALUE;
+        private int mMinIdx = -1;
+        private String mLabel;
 
         /**
          * returns next scheduled alert, MAX_VALUE if none
@@ -492,6 +495,9 @@ public class Alarms {
         public int getIndex() {
             return mMinIdx;
         }
+        public String getLabel() {
+            return mLabel;
+        }
 
         public void reportAlarm(
                 int idx, boolean enabled, int hour, int minutes,
@@ -505,6 +511,7 @@ public class Alarms {
                 if (atTime < mMinAlert) {
                     mMinIdx = idx;
                     mMinAlert = atTime;
+                    mLabel = message;
                 }
             }
         }
@@ -564,7 +571,7 @@ public class Alarms {
             long atTime = ac.getAlert();
 
             if (atTime < Long.MAX_VALUE) {
-                enableAlert(context, id, atTime);
+                enableAlert(context, id, ac.getLabel(), atTime);
             } else {
                 disableAlert(context, id);
             }
@@ -586,13 +593,15 @@ public class Alarms {
      * @param id Alarm ID.
      * @param atTimeInMillis milliseconds since epoch
      */
-    static void enableAlert(Context context, int id, long atTimeInMillis) {
+    static void enableAlert(Context context, int id, String label,
+           long atTimeInMillis) {
         AlarmManager am = (AlarmManager)
                 context.getSystemService(Context.ALARM_SERVICE);
 
         Intent intent = new Intent(ALARM_ALERT_ACTION);
         if (Log.LOGV) Log.v("** setAlert id " + id + " atTime " + atTimeInMillis);
         intent.putExtra(ID, id);
+        intent.putExtra(LABEL, label);
         intent.putExtra(TIME, atTimeInMillis);
         PendingIntent sender = PendingIntent.getBroadcast(
                 context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
@@ -631,12 +640,13 @@ public class Alarms {
     }
 
     static void saveSnoozeAlert(final Context context, int id,
-                                long atTimeInMillis) {
+                                long atTimeInMillis, String label) {
         SharedPreferences prefs = context.getSharedPreferences(
                 AlarmClock.PREFERENCES, 0);
         SharedPreferences.Editor ed = prefs.edit();
         ed.putInt(PREF_SNOOZE_ID, id);
         ed.putLong(PREF_SNOOZE_TIME, atTimeInMillis);
+        ed.putString(PREF_SNOOZE_LABEL, label);
         ed.commit();
     }
 
@@ -646,7 +656,7 @@ public class Alarms {
     static int disableSnoozeAlert(final Context context) {
         int id = getSnoozeAlarmId(context);
         if (id == -1) return -1;
-        saveSnoozeAlert(context, -1, 0);
+        saveSnoozeAlert(context, -1, 0, null);
         return id;
     }
 
@@ -671,7 +681,13 @@ public class Alarms {
         if (id == -1) return false;
         long atTimeInMillis = prefs.getLong(PREF_SNOOZE_TIME, -1);
         if (id == -1) return false;
-        enableAlert(context, id, atTimeInMillis);
+        // Try to get the label from the snooze preference. If null, use the
+        // default label.
+        String label = prefs.getString(PREF_SNOOZE_LABEL, null);
+        if (label == null) {
+            label = context.getString(R.string.default_label);
+        }
+        enableAlert(context, id, label, atTimeInMillis);
         return true;
     }
 
index d8c9b46..807b469 100644 (file)
@@ -27,9 +27,10 @@ import android.media.RingtoneManager;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
+import android.preference.CheckBoxPreference;
+import android.preference.EditTextPreference;
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
-import android.preference.CheckBoxPreference;
 import android.preference.PreferenceScreen;
 import android.text.format.DateFormat;
 import android.view.Menu;
@@ -43,6 +44,7 @@ import android.widget.Toast;
 public class SetAlarm extends PreferenceActivity
         implements Alarms.AlarmSettings, TimePickerDialog.OnTimeSetListener {
 
+    private EditTextPreference mLabel;
     private CheckBoxPreference mAlarmOnPref;
     private Preference mTimePref;
     private AlarmPreference mAlarmPref;
@@ -98,6 +100,16 @@ public class SetAlarm extends PreferenceActivity
         super.onCreate(icicle);
 
         addPreferencesFromResource(R.xml.alarm_prefs);
+        mLabel = (EditTextPreference) findPreference("label");
+        mLabel.setOnPreferenceChangeListener(
+                new Preference.OnPreferenceChangeListener() {
+                    public boolean onPreferenceChange(Preference p,
+                            Object newValue) {
+                        p.setSummary((String) newValue);
+                        saveAlarm(false, (String) newValue);
+                        return true;
+                    }
+                });
         mAlarmOnPref = (CheckBoxPreference)findPreference("on");
         mTimePref = findPreference("time");
         mAlarmPref = (AlarmPreference) findPreference("alarm");
@@ -193,9 +205,14 @@ public class SetAlarm extends PreferenceActivity
      */
     public void reportAlarm(
             int idx, boolean enabled, int hour, int minutes,
-            Alarms.DaysOfWeek daysOfWeek, boolean vibrate,String message,
+            Alarms.DaysOfWeek daysOfWeek, boolean vibrate, String label,
             String alert) {
 
+        if (label == null || label.length() == 0) {
+            label = getString(R.string.default_label);
+        }
+        mLabel.setText(label);
+        mLabel.setSummary(label);
         mHour = hour;
         mMinutes = minutes;
         mAlarmOnPref.setChecked(enabled);
@@ -251,10 +268,18 @@ public class SetAlarm extends PreferenceActivity
     }
 
     private void saveAlarm(boolean popToast) {
+        saveAlarm(popToast, mLabel.getText());
+    }
+
+    /**
+     * This version of saveAlarm uses the passed in label since mLabel may
+     * contain the old value (i.e. during the preference value change).
+     */
+    private void saveAlarm(boolean popToast, String label) {
         if (mReportAlarmCalled && mAlarmPref.mAlert != null) {
             String alertString = mAlarmPref.mAlert.toString();
             saveAlarm(this, mId, mAlarmOnPref.isChecked(), mHour, mMinutes,
-                      mDaysOfWeek, mVibratePref.isChecked(), alertString,
+                      mDaysOfWeek, mVibratePref.isChecked(), label, alertString,
                       popToast);
         }
     }
@@ -265,14 +290,14 @@ public class SetAlarm extends PreferenceActivity
      */
     private static void saveAlarm(
             Context context, int id, boolean enabled, int hour, int minute,
-            Alarms.DaysOfWeek daysOfWeek, boolean vibrate, String alert,
-            boolean popToast) {
-        if (Log.LOGV) Log.v("** saveAlarm " + id + " " + enabled + " " + hour +
-                            " " + minute + " vibe " + vibrate);
+            Alarms.DaysOfWeek daysOfWeek, boolean vibrate, String label,
+            String alert, boolean popToast) {
+        if (Log.LOGV) Log.v("** saveAlarm " + id + " " + label + " " + enabled
+                + " " + hour + " " + minute + " vibe " + vibrate);
 
         // Fix alert string first
         Alarms.setAlarm(context, id, enabled, hour, minute, daysOfWeek, vibrate,
-                        "", alert);
+                label, alert);
 
         if (enabled && popToast) {
             popAlarmSetToast(context, hour, minute, daysOfWeek);
@@ -392,7 +417,7 @@ public class SetAlarm extends PreferenceActivity
         int hour = nowHour + (nowMinute == 0? 1 : 0);
 
         saveAlarm(this, mId, true, hour, minutes, mDaysOfWeek, true,
-                  mAlarmPref.mAlert.toString(), true);
+                mLabel.getText(), mAlarmPref.mAlert.toString(), true);
     }
 
 }