OSDN Git Service

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