OSDN Git Service

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