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.DatePickerDialog;
20 import android.content.DialogInterface;
21 import android.util.AndroidRuntimeException;
22 import android.widget.DatePicker;
23 
24 import org.json.JSONException;
25 import org.json.JSONObject;
26 
27 /**
28  * Wrapper class for date picker dialog running in separate thread.
29  *
30  */
31 public class DatePickerDialogTask extends DialogTask {
32   public static int mYear;
33   public static int mMonth;
34   public static int mDay;
35 
36   public DatePickerDialogTask(int year, int month, int day) {
37     mYear = year;
38     mMonth = month - 1;
39     mDay = day;
40   }
41 
42   @Override
43   public void onCreate() {
44     super.onCreate();
45     mDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
46       @Override
47       public void onDateSet(DatePicker view, int year, int month, int day) {
48         JSONObject result = new JSONObject();
49         try {
50           result.put("which", "positive");
51           result.put("year", year);
52           result.put("month", month + 1);
53           result.put("day", day);
54           setResult(result);
55         } catch (JSONException e) {
56           throw new AndroidRuntimeException(e);
57         }
58       }
59     }, mYear, mMonth, mDay);
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("year", mYear);
67           result.put("month", mMonth + 1);
68           result.put("day", mDay);
69           setResult(result);
70         } catch (JSONException e) {
71           throw new AndroidRuntimeException(e);
72         }
73       }
74     });
75     mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
76       @Override
77       public void onDismiss(DialogInterface dialog) {
78         JSONObject result = new JSONObject();
79         try {
80           result.put("which", "negative");
81           result.put("year", mYear);
82           result.put("month", mMonth + 1);
83           result.put("day", mDay);
84           setResult(result);
85         } catch (JSONException e) {
86           throw new AndroidRuntimeException(e);
87         }
88       }
89     });
90     mDialog.show();
91     mShowLatch.countDown();
92   }
93 }
94