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