Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2017 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 
     17 package com.googlecode.android_scripting.facade.ui;
     18 
     19 import android.app.TimePickerDialog;
     20 import android.content.DialogInterface;
     21 import android.util.AndroidRuntimeException;
     22 import android.widget.TimePicker;
     23 
     24 import org.json.JSONException;
     25 import org.json.JSONObject;
     26 
     27 /**
     28  * Wrapper class for time picker dialog running in separate thread.
     29  *
     30  */
     31 public class TimePickerDialogTask extends DialogTask {
     32   private final int mHour;
     33   private final int mMinute;
     34   private final boolean mIs24Hour;
     35 
     36   public TimePickerDialogTask(int hour, int minute, boolean is24hour) {
     37     mHour = hour;
     38     mMinute = minute;
     39     mIs24Hour = is24hour;
     40   }
     41 
     42   @Override
     43   public void onCreate() {
     44     super.onCreate();
     45     mDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
     46       @Override
     47       public void onTimeSet(TimePicker view, int hour, int minute) {
     48         JSONObject result = new JSONObject();
     49         try {
     50           result.put("which", "positive");
     51           result.put("hour", hour);
     52           result.put("minute", minute);
     53           setResult(result);
     54         } catch (JSONException e) {
     55           throw new AndroidRuntimeException(e);
     56         }
     57       }
     58     }, mHour, mMinute, mIs24Hour);
     59     mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
     60       @Override
     61       public void onCancel(DialogInterface view) {
     62         JSONObject result = new JSONObject();
     63         try {
     64           result.put("which", "neutral");
     65           result.put("hour", mHour);
     66           result.put("minute", mMinute);
     67           setResult(result);
     68         } catch (JSONException e) {
     69           throw new AndroidRuntimeException(e);
     70         }
     71       }
     72     });
     73     mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
     74       @Override
     75       public void onDismiss(DialogInterface dialog) {
     76         JSONObject result = new JSONObject();
     77         try {
     78           result.put("which", "negative");
     79           result.put("hour", mHour);
     80           result.put("minute", mMinute);
     81           setResult(result);
     82         } catch (JSONException e) {
     83           throw new AndroidRuntimeException(e);
     84         }
     85       }
     86     });
     87     mDialog.show();
     88     mShowLatch.countDown();
     89   }
     90 }
     91