• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Activity;
20 import android.app.AlertDialog;
21 import android.app.AlertDialog.Builder;
22 import android.content.DialogInterface;
23 import android.util.AndroidRuntimeException;
24 import android.widget.SeekBar;
25 
26 import com.googlecode.android_scripting.Log;
27 import com.googlecode.android_scripting.facade.EventFacade;
28 
29 import org.json.JSONException;
30 import org.json.JSONObject;
31 
32 /**
33  * Wrapper class for dialog box with seek bar.
34  *
35  * @author MeanEYE.rcf (meaneye.rcf@gmail.com)
36  */
37 public class SeekBarDialogTask extends DialogTask {
38 
39   private SeekBar mSeekBar;
40   private final int mProgress;
41   private final int mMax;
42   private final String mTitle;
43   private final String mMessage;
44   private String mPositiveButtonText;
45   private String mNegativeButtonText;
46 
SeekBarDialogTask(int progress, int max, String title, String message)47   public SeekBarDialogTask(int progress, int max, String title, String message) {
48     mProgress = progress;
49     mMax = max;
50     mTitle = title;
51     mMessage = message;
52   }
53 
setPositiveButtonText(String text)54   public void setPositiveButtonText(String text) {
55     mPositiveButtonText = text;
56   }
57 
setNegativeButtonText(String text)58   public void setNegativeButtonText(String text) {
59     mNegativeButtonText = text;
60   }
61 
62   @Override
onCreate()63   public void onCreate() {
64     super.onCreate();
65     mSeekBar = new SeekBar(getActivity());
66     mSeekBar.setMax(mMax);
67     mSeekBar.setProgress(mProgress);
68     mSeekBar.setPadding(10, 0, 10, 3);
69     mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
70 
71       @Override
72       public void onStopTrackingTouch(SeekBar arg0) {
73       }
74 
75       @Override
76       public void onStartTrackingTouch(SeekBar arg0) {
77       }
78 
79       @Override
80       public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
81         EventFacade eventFacade = getEventFacade();
82         if (eventFacade != null) {
83           JSONObject result = new JSONObject();
84           try {
85             result.put("which", "seekbar");
86             result.put("progress", mSeekBar.getProgress());
87             result.put("fromuser", fromUser);
88             eventFacade.postEvent("dialog", result);
89           } catch (JSONException e) {
90             Log.e(e);
91           }
92         }
93       }
94     });
95 
96     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
97     if (mTitle != null) {
98       builder.setTitle(mTitle);
99     }
100     if (mMessage != null) {
101       builder.setMessage(mMessage);
102     }
103     builder.setView(mSeekBar);
104     configureButtons(builder, getActivity());
105     addOnCancelListener(builder, getActivity());
106     mDialog = builder.show();
107     mShowLatch.countDown();
108   }
109 
addOnCancelListener(final AlertDialog.Builder builder, final Activity activity)110   private Builder addOnCancelListener(final AlertDialog.Builder builder, final Activity activity) {
111     return builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
112       @Override
113       public void onCancel(DialogInterface dialog) {
114         JSONObject result = new JSONObject();
115         try {
116           result.put("canceled", true);
117           result.put("progress", mSeekBar.getProgress());
118         } catch (JSONException e) {
119           Log.e(e);
120         }
121         dismissDialog();
122         setResult(result);
123       }
124     });
125   }
126 
127   private void configureButtons(final AlertDialog.Builder builder, final Activity activity) {
128     DialogInterface.OnClickListener buttonListener = new DialogInterface.OnClickListener() {
129       @Override
130       public void onClick(DialogInterface dialog, int which) {
131         JSONObject result = new JSONObject();
132         switch (which) {
133         case DialogInterface.BUTTON_POSITIVE:
134           try {
135             result.put("which", "positive");
136             result.put("progress", mSeekBar.getProgress());
137           } catch (JSONException e) {
138             throw new AndroidRuntimeException(e);
139           }
140           break;
141         case DialogInterface.BUTTON_NEGATIVE:
142           try {
143             result.put("which", "negative");
144             result.put("progress", mSeekBar.getProgress());
145           } catch (JSONException e) {
146             throw new AndroidRuntimeException(e);
147           }
148           break;
149         }
150         dismissDialog();
151         setResult(result);
152       }
153     };
154     if (mNegativeButtonText != null) {
155       builder.setNegativeButton(mNegativeButtonText, buttonListener);
156     }
157     if (mPositiveButtonText != null) {
158       builder.setPositiveButton(mPositiveButtonText, buttonListener);
159     }
160   }
161 }
162