OSDN Git Service

DataEditDialogのDialogFragment化(Ambient時に操作できない不具合を改修)
[gokigen/JoggingTimer.git] / wear / src / main / java / net / osdn / gokigen / joggingtimer / utilities / DataEditDialog.java
1 package net.osdn.gokigen.joggingtimer.utilities;
2
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.app.Dialog;
6 import android.app.DialogFragment;
7 import android.content.Context;
8 import android.content.DialogInterface;
9 import android.content.res.TypedArray;
10 import android.os.Bundle;
11 import android.support.annotation.NonNull;
12 import android.util.Log;
13 import android.view.LayoutInflater;
14 import android.view.View;
15 import android.view.ViewGroup;
16 import android.widget.AdapterView;
17 import android.widget.ArrayAdapter;
18 import android.widget.EditText;
19 import android.widget.Spinner;
20 import android.widget.TextView;
21
22 import net.osdn.gokigen.joggingtimer.R;
23
24 /**
25  *
26  *
27  */
28 public class DataEditDialog  extends DialogFragment
29 {
30     private final String TAG = toString();
31     private int iconResId = 0;
32     private String title = "";
33     private int selectedPosition = 0;
34     private DataEditDialog.Callback callback = null;
35     Dialog myDialog = null;
36
37     /**
38      *
39      *
40      */
41     public static DataEditDialog newInstance(int iconResId, String title,  @NonNull DataEditDialog.Callback callback)
42     {
43         DataEditDialog instance = new DataEditDialog();
44         instance.prepare(iconResId, title, callback);
45
46         // パラメータはBundleにまとめておく
47         Bundle arguments = new Bundle();
48         arguments.putString("title", title);
49         //arguments.putString("message", message);
50         instance.setArguments(arguments);
51
52         return (instance);
53     }
54
55     /**
56      *
57      *
58      */
59     private void prepare(int iconResId, String title,  @NonNull DataEditDialog.Callback callback)
60     {
61         this.iconResId = iconResId;
62         this.title = title;
63         this.callback = callback;
64     }
65
66     /**
67      *
68      *
69      */
70     @Override
71     public @NonNull Dialog onCreateDialog(Bundle savedInstanceState)
72     {
73         Activity activity = getActivity();
74
75         // 確認ダイアログの生成
76         final AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
77
78         // Get the layout inflater
79         LayoutInflater inflater = activity.getLayoutInflater();
80         final View alertView = inflater.inflate(R.layout.information_dialog, null, false);
81         alertDialog.setView(alertView);
82
83         final String[] objects = activity.getResources().getStringArray(R.array.icon_selection_id);
84         final Spinner spinner = alertView.findViewById(R.id.spinner_selection);
85         final EditText titleText = alertView.findViewById(R.id.edit_title);
86         try
87         {
88             titleText.setText(title);
89             IconListAdapter adapter = new IconListAdapter(activity, R.layout.icon_list, objects);
90             spinner.setAdapter(adapter);
91             spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
92                 @Override
93                 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
94                     Log.v(TAG, "onItemSelected : " + position + " (" + id + ")");
95                     selectedPosition = position;
96                 }
97
98                 @Override
99                 public void onNothingSelected(AdapterView<?> parent) {
100                     Log.v(TAG, "onNothingSelected");
101                 }
102             });
103         }
104         catch (Exception e)
105         {
106             e.printStackTrace();
107         }
108         alertDialog.setIcon(iconResId);
109         alertDialog.setMessage(activity.getString(R.string.dialog_message_data_edit));
110         alertDialog.setCancelable(true);
111
112         // ボタンを設定する(実行ボタン)
113         alertDialog.setPositiveButton(activity.getString(R.string.dialog_positive_execute),
114                 new DialogInterface.OnClickListener() {
115                     public void onClick(DialogInterface dialog, int which)
116                     {
117                         try
118                         {
119                             Activity activity = getActivity();
120                             if (activity != null)
121                             {
122                                 String array[] = activity.getResources().getStringArray(R.array.icon_selection_id);
123                                 if (callback != null)
124                                 {
125                                     callback.dataEdited(Integer.parseInt(array[selectedPosition]), titleText.getText().toString());
126                                 }
127                             }
128                         }
129                         catch (Exception e)
130                         {
131                             e.printStackTrace();
132                             if (callback != null)
133                             {
134                                 callback.cancelled();
135                             }
136                         }
137                         dialog.dismiss();
138                     }
139                 });
140
141         // ボタンを設定する (キャンセルボタン)
142         alertDialog.setNegativeButton(activity.getString(R.string.dialog_negative_cancel),
143                 new DialogInterface.OnClickListener() {
144                     public void onClick(DialogInterface dialog, int which) {
145                         if (callback != null)
146                         {
147                             callback.cancelled();
148                         }
149                         dialog.cancel();
150                     }
151                 });
152
153         // 確認ダイアログを応答する
154         myDialog = alertDialog.create();
155         return (myDialog);
156     }
157
158
159     @Override
160     public void onPause()
161     {
162         super.onPause();
163         Log.v(TAG, "AlertDialog::onPause()");
164         if (myDialog != null)
165         {
166             myDialog.cancel();
167         }
168     }
169
170     /**
171      * コールバックインタフェース
172      *
173      */
174     public interface Callback
175     {
176         void dataEdited(int iconId, String title); // OKを選択したとき
177         void cancelled();                          // キャンセルしたとき
178     }
179
180     public class IconListAdapter extends ArrayAdapter<String>
181     {
182         private final LayoutInflater inflater;
183         private final int layoutResourceId;
184         private final String[]  stringList;
185
186         private IconListAdapter(Activity activity, int layoutResourceId, String[] strings)
187         {
188             super(activity, layoutResourceId, strings);
189             inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
190             stringList = strings;
191             this.layoutResourceId = layoutResourceId;
192         }
193
194         @Override
195         public int getCount()
196         {
197             return stringList.length;
198         }
199
200         @Override
201         public String getItem(int position)
202         {
203             return stringList[position];
204         }
205
206         @Override
207         public long getItemId(int position)
208         {
209             return (position);
210         }
211
212         @Override
213         public @NonNull View getView(int position, View convertView, @NonNull ViewGroup parent)
214         {
215             View row = inflater.inflate(layoutResourceId, parent, false);
216             try
217             {
218                 TextView target = row.findViewById(R.id.selection_icon);
219                 TypedArray imgs = getActivity().getResources().obtainTypedArray(R.array.icon_selection);
220                 int rscId = imgs.getResourceId(position, R.drawable.ic_label_outline_black_24dp);
221                 target.setCompoundDrawablesWithIntrinsicBounds(rscId, 0, 0, 0);
222                 imgs.recycle();
223             }
224             catch (Exception e)
225             {
226                 e.printStackTrace();
227             }
228             return (row);
229         }
230
231         @Override
232         public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent)
233         {
234             return (getView(position, convertView, parent));
235         }
236     }
237 }