1 package net.osdn.gokigen.a01d.preference.canon;
3 import android.content.Context;
4 import android.content.Intent;
5 import android.content.SharedPreferences;
6 import android.os.Bundle;
7 import android.provider.Settings;
8 import android.util.Log;
10 import androidx.annotation.NonNull;
11 import androidx.appcompat.app.AppCompatActivity;
12 import androidx.fragment.app.FragmentActivity;
13 import androidx.preference.CheckBoxPreference;
14 import androidx.preference.ListPreference;
15 import androidx.preference.Preference;
16 import androidx.preference.PreferenceFragmentCompat;
17 import androidx.preference.PreferenceManager;
19 import net.osdn.gokigen.a01d.IChangeScene;
20 import net.osdn.gokigen.a01d.R;
21 import net.osdn.gokigen.a01d.camera.ptpip.operation.PtpIpCameraPowerOff;
22 import net.osdn.gokigen.a01d.logcat.LogCatViewer;
23 import net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor;
27 import static net.osdn.gokigen.a01d.preference.IPreferencePropertyAccessor.WIFI_SETTINGS;
33 public class CanonPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener
35 private final String TAG = toString();
36 private AppCompatActivity context = null;
37 private SharedPreferences preferences = null;
38 private PtpIpCameraPowerOff powerOffController = null;
39 private LogCatViewer logCatViewer = null;
45 public static CanonPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
47 CanonPreferenceFragment instance = new CanonPreferenceFragment();
48 instance.prepare(context, changeScene);
50 // パラメータはBundleにまとめておく
51 Bundle arguments = new Bundle();
52 //arguments.putString("title", title);
53 //arguments.putString("message", message);
54 instance.setArguments(arguments);
63 private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
67 powerOffController = new PtpIpCameraPowerOff(context, changeScene);
68 powerOffController.prepare();
70 logCatViewer = new LogCatViewer(changeScene);
71 logCatViewer.prepare();
73 //cameraApiListViewer = new PanasonicCameraApiListViewer(changeScene);
74 //cameraApiListViewer.prepare();
76 this.context = context;
89 public void onAttach(@NonNull Context activity)
91 super.onAttach(activity);
92 Log.v(TAG, "onAttach()");
97 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
100 initializePreferences();
102 preferences.registerOnSharedPreferenceChangeListener(this);
114 private void initializePreferences()
118 Map<String, ?> items = preferences.getAll();
119 SharedPreferences.Editor editor = preferences.edit();
121 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA)) {
122 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
124 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
125 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
127 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
128 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
143 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
145 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
151 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
152 value = preferences.getBoolean(key, true);
153 Log.v(TAG, " " + key + " , " + value);
156 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
157 value = preferences.getBoolean(key, true);
158 Log.v(TAG, " " + key + " , " + value);
162 String strValue = preferences.getString(key, "");
163 setListPreference(key, key, strValue);
174 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
176 Log.v(TAG, "onCreatePreferences()");
179 //super.onCreate(savedInstanceState);
180 addPreferencesFromResource(R.xml.preferences_olympuspen);
182 ListPreference connectionMethod = findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
183 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
185 public boolean onPreferenceChange(Preference preference, Object newValue) {
186 preference.setSummary(newValue + " ");
190 connectionMethod.setSummary(connectionMethod.getValue() + " ");
192 findPreference("exit_application").setOnPreferenceClickListener(powerOffController);
193 findPreference("debug_info").setOnPreferenceClickListener(logCatViewer);
194 //findPreference("panasonic_api_list").setOnPreferenceClickListener(cameraApiListViewer);
195 findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
208 public void onResume()
211 Log.v(TAG, "onResume() Start");
215 synchronizedProperty();
222 Log.v(TAG, "onResume() End");
231 public void onPause()
234 Log.v(TAG, "onPause() Start");
238 // Preference変更のリスナを解除
239 preferences.unregisterOnSharedPreferenceChangeListener(this);
246 Log.v(TAG, "onPause() End");
250 * ListPreference の表示データを設定
252 * @param pref_key Preference(表示)のキー
253 * @param key Preference(データ)のキー
254 * @param defaultValue Preferenceのデフォルト値
256 private void setListPreference(String pref_key, String key, String defaultValue)
261 pref = (ListPreference) findPreference(pref_key);
262 String value = preferences.getString(key, defaultValue);
265 pref.setValue(value);
266 pref.setSummary(value);
276 * BooleanPreference の表示データを設定
278 * @param pref_key Preference(表示)のキー
279 * @param key Preference(データ)のキー
280 * @param defaultValue Preferenceのデフォルト値
282 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
286 CheckBoxPreference pref = findPreference(pref_key);
288 boolean value = preferences.getBoolean(key, defaultValue);
289 pref.setChecked(value);
302 private void synchronizedProperty()
304 final FragmentActivity activity = getActivity();
305 final boolean defaultValue = true;
306 if (activity != null)
308 activity.runOnUiThread(new Runnable() {
313 // Preferenceの画面に反映させる
314 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, defaultValue);
315 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, defaultValue);
327 public boolean onPreferenceClick(Preference preference)
331 String preferenceKey = preference.getKey();
332 if (preferenceKey.contains(WIFI_SETTINGS))
335 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
338 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));