[Android] fix TimeSleepDialog

Sébastien Toque git at videolan.org
Sun Dec 30 19:30:25 CET 2012


vlc-ports/android | branch: master | Sébastien Toque <xilasz at gmail.com> | Fri Dec 28 23:01:58 2012 +0100| [3ba1aa1056f5e5b146ba4598864e4804e3296cd7] | committer: Sébastien Toque

fix TimeSleepDialog

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=3ba1aa1056f5e5b146ba4598864e4804e3296cd7
---

 .../src/org/videolan/vlc/gui/TimeSleepDialog.java  |   69 ++++++++++----------
 1 file changed, 36 insertions(+), 33 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/TimeSleepDialog.java b/vlc-android/src/org/videolan/vlc/gui/TimeSleepDialog.java
index c927378..b22188d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/TimeSleepDialog.java
+++ b/vlc-android/src/org/videolan/vlc/gui/TimeSleepDialog.java
@@ -37,58 +37,61 @@ import android.widget.TimePicker;
 public class TimeSleepDialog extends TimePickerDialog {
     public final static String TAG = "VLC/TimeSleepDialog";
 
-    private static PendingIntent mSleepPendingIntent = null;
-    private static Context mContext;
+    private int hourOfDay;
+    protected int minute;
 
     private TimeSleepDialog(Context context,
             OnTimeSetListener callBack, int hourOfDay, int minute,
             boolean is24HourView) {
         super(context, callBack, hourOfDay, minute, is24HourView);
-
     }
 
-    public TimeSleepDialog (Context context, int hourOfDay, int minute) {
-        this(context, onTimeSetListener (context), hourOfDay, minute, true);
+    public TimeSleepDialog(Context context, int hourOfDay, int minute) {
+        this(context, null, hourOfDay, minute, true);
         setCanceledOnTouchOutside(true);
         setTitle(R.string.sleep_title);
+        setButton(AlertDialog.BUTTON_POSITIVE, context.getString(R.string.sleep_title), mSleepListener);
         setButton(AlertDialog.BUTTON_NEUTRAL, context.getString(R.string.sleep_cancel), mSleepCancelListener);
         show();
     }
 
-    private static OnTimeSetListener onTimeSetListener (Context context) {
-        mContext = context;
-        return new OnTimeSetListener() {
-            @Override
-            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
-                AlarmManager alarmMgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
-                Intent intent = new Intent(VLCApplication.SLEEP_INTENT);
-                mSleepPendingIntent = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+    @Override
+    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
+        super.onTimeChanged(view, hourOfDay, minute);
+        this.hourOfDay = hourOfDay;
+        this.minute = minute;
+    }
 
-                Calendar currentTime = Calendar.getInstance();
-                currentTime.setTimeInMillis(System.currentTimeMillis());
-                Calendar sleepTime = Calendar.getInstance();
-                sleepTime.setTimeInMillis(System.currentTimeMillis());
-                sleepTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
-                sleepTime.set(Calendar.MINUTE, minute);
-                sleepTime.set(Calendar.SECOND, 0);
-                if(sleepTime.before(currentTime))
-                    sleepTime.roll(Calendar.DATE, true);
+    private final DialogInterface.OnClickListener mSleepListener = new OnClickListener() {
+        @Override
+        public void onClick(DialogInterface dialog, int which) {
+            AlarmManager alarmMgr = (AlarmManager) TimeSleepDialog.this.getContext().getSystemService(Context.ALARM_SERVICE);
+            Intent intent = new Intent(VLCApplication.SLEEP_INTENT);
+            PendingIntent sleepPendingIntent = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 
-                Log.i(TAG, "VLC will sleep at " + sleepTime.getTime().toString());
-                alarmMgr.set(AlarmManager.RTC_WAKEUP, sleepTime.getTimeInMillis(), mSleepPendingIntent);
-            }
-        };
-    }
+            Calendar currentTime = Calendar.getInstance();
+            currentTime.setTimeInMillis(System.currentTimeMillis());
+            Calendar sleepTime = Calendar.getInstance();
+            sleepTime.setTimeInMillis(System.currentTimeMillis());
+            sleepTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
+            sleepTime.set(Calendar.MINUTE, minute);
+            sleepTime.set(Calendar.SECOND, 0);
+            if (sleepTime.before(currentTime))
+                sleepTime.roll(Calendar.DATE, true);
 
-    private final DialogInterface.OnClickListener mSleepCancelListener = new DialogInterface.OnClickListener() {
+            Log.i(TAG, "VLC will sleep at " + sleepTime.getTime().toString());
+            alarmMgr.set(AlarmManager.RTC_WAKEUP, sleepTime.getTimeInMillis(), sleepPendingIntent);
+        }
+    };
 
+    private final DialogInterface.OnClickListener mSleepCancelListener = new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
-            if(mSleepPendingIntent != null) {
-                Log.i(TAG, "Sleep cancelled");
-                AlarmManager alarmMgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
-                alarmMgr.cancel(mSleepPendingIntent);
-            }
+            Log.i(TAG, "Sleep cancelled");
+            AlarmManager alarmMgr = (AlarmManager) TimeSleepDialog.this.getContext().getSystemService(Context.ALARM_SERVICE);
+            Intent intent = new Intent(VLCApplication.SLEEP_INTENT);
+            PendingIntent sleepPendingIntent = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+            alarmMgr.cancel(sleepPendingIntent);
         }
     };
 }



More information about the Android mailing list