1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * 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 * @author MeanEYE.rcf (meaneye.rcf@gmail.com) 31 */ 32 public class TimePickerDialogTask extends DialogTask { 33 private final int mHour; 34 private final int mMinute; 35 private final boolean mIs24Hour; 36 TimePickerDialogTask(int hour, int minute, boolean is24hour)37 public TimePickerDialogTask(int hour, int minute, boolean is24hour) { 38 mHour = hour; 39 mMinute = minute; 40 mIs24Hour = is24hour; 41 } 42 43 @Override onCreate()44 public void onCreate() { 45 super.onCreate(); 46 mDialog = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() { 47 @Override 48 public void onTimeSet(TimePicker view, int hour, int minute) { 49 JSONObject result = new JSONObject(); 50 try { 51 result.put("which", "positive"); 52 result.put("hour", hour); 53 result.put("minute", minute); 54 setResult(result); 55 } catch (JSONException e) { 56 throw new AndroidRuntimeException(e); 57 } 58 } 59 }, mHour, mMinute, mIs24Hour); 60 mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 61 @Override 62 public void onCancel(DialogInterface view) { 63 JSONObject result = new JSONObject(); 64 try { 65 result.put("which", "neutral"); 66 result.put("hour", mHour); 67 result.put("minute", mMinute); 68 setResult(result); 69 } catch (JSONException e) { 70 throw new AndroidRuntimeException(e); 71 } 72 } 73 }); 74 mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { 75 @Override 76 public void onDismiss(DialogInterface dialog) { 77 JSONObject result = new JSONObject(); 78 try { 79 result.put("which", "negative"); 80 result.put("hour", mHour); 81 result.put("minute", mMinute); 82 setResult(result); 83 } catch (JSONException e) { 84 throw new AndroidRuntimeException(e); 85 } 86 } 87 }); 88 mDialog.show(); 89 mShowLatch.countDown(); 90 } 91 } 92