1 package net.osdn.gokigen.pkremote.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;
12 import androidx.annotation.NonNull;
13 import androidx.appcompat.app.AppCompatActivity;
14 import androidx.fragment.app.FragmentActivity;
15 import androidx.preference.CheckBoxPreference;
16 import androidx.preference.ListPreference;
17 import androidx.preference.Preference;
18 import androidx.preference.PreferenceFragmentCompat;
19 import androidx.preference.PreferenceManager;
21 import net.osdn.gokigen.pkremote.R;
22 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.operation.PtpIpCameraPowerOff;
23 import net.osdn.gokigen.pkremote.logcat.LogCatViewer;
24 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
25 import net.osdn.gokigen.pkremote.scene.IChangeScene;
27 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.DEBUG_INFO;
28 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.EXIT_APPLICATION;
29 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.WIFI_SETTINGS;
35 public class CanonPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener
37 private final String TAG = toString();
38 private AppCompatActivity context = null;
39 private SharedPreferences preferences = null;
40 private PtpIpCameraPowerOff powerOffController = null;
41 private LogCatViewer logCatViewer = null;
47 public static CanonPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
49 CanonPreferenceFragment instance = new CanonPreferenceFragment();
50 instance.prepare(context, changeScene);
52 // パラメータはBundleにまとめておく
53 Bundle arguments = new Bundle();
54 //arguments.putString("title", title);
55 //arguments.putString("message", message);
56 instance.setArguments(arguments);
65 private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
69 powerOffController = new PtpIpCameraPowerOff(context, changeScene);
70 powerOffController.prepare();
72 logCatViewer = new LogCatViewer(changeScene);
73 logCatViewer.prepare();
75 this.context = context;
88 public void onAttach(Context activity)
90 super.onAttach(activity);
91 Log.v(TAG, "onAttach()");
96 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
99 initializePreferences();
101 preferences.registerOnSharedPreferenceChangeListener(this);
113 private void initializePreferences()
117 Map<String, ?> items = preferences.getAll();
118 SharedPreferences.Editor editor = preferences.edit();
120 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA)) {
121 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
123 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW)) {
124 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
126 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD)) {
127 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
129 if (!items.containsKey(IPreferencePropertyAccessor.CANON_RAW_SUFFIX)) {
130 editor.putString(IPreferencePropertyAccessor.CANON_RAW_SUFFIX, IPreferencePropertyAccessor.CANON_RAW_SUFFIX_DEFAULT_VALUE);
132 if (!items.containsKey(IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL)) {
133 editor.putBoolean(IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL, false);
148 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
150 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
156 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
157 value = preferences.getBoolean(key, true);
158 Log.v(TAG, " " + key + " , " + value);
161 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
162 value = preferences.getBoolean(key, true);
163 Log.v(TAG, " " + key + " , " + value);
166 case IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL:
167 value = preferences.getBoolean(key, false);
168 Log.v(TAG, " " + key + " , " + value);
172 String strValue = preferences.getString(key, "");
173 setListPreference(key, key, strValue);
184 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
186 Log.v(TAG, "onCreatePreferences()");
189 //super.onCreate(savedInstanceState);
190 addPreferencesFromResource(R.xml.preferences_canon);
192 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
193 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
195 public boolean onPreferenceChange(Preference preference, Object newValue) {
196 preference.setSummary(newValue + " ");
200 connectionMethod.setSummary(connectionMethod.getValue() + " ");
202 findPreference(EXIT_APPLICATION).setOnPreferenceClickListener(powerOffController);
203 findPreference(DEBUG_INFO).setOnPreferenceClickListener(logCatViewer);
204 findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
217 public void onResume()
220 Log.v(TAG, "onResume() Start");
223 synchronizedProperty();
229 Log.v(TAG, "onResume() End");
237 public void onPause()
240 Log.v(TAG, "onPause() Start");
244 // Preference変更のリスナを解除
245 preferences.unregisterOnSharedPreferenceChangeListener(this);
252 Log.v(TAG, "onPause() End");
256 * ListPreference の表示データを設定
258 * @param pref_key Preference(表示)のキー
259 * @param key Preference(データ)のキー
260 * @param defaultValue Preferenceのデフォルト値
262 private void setListPreference(String pref_key, String key, String defaultValue)
267 pref = (ListPreference) findPreference(pref_key);
268 String value = preferences.getString(key, defaultValue);
271 pref.setValue(value);
272 pref.setSummary(value);
282 * BooleanPreference の表示データを設定
284 * @param pref_key Preference(表示)のキー
285 * @param key Preference(データ)のキー
286 * @param defaultValue Preferenceのデフォルト値
288 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
292 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
294 boolean value = preferences.getBoolean(key, defaultValue);
295 pref.setChecked(value);
308 private void synchronizedProperty()
310 final FragmentActivity activity = getActivity();
311 if (activity != null)
313 activity.runOnUiThread(new Runnable() {
318 // Preferenceの画面に反映させる
319 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
320 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
321 setBooleanPreference(IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL, IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL, false);
333 public boolean onPreferenceClick(Preference preference)
337 String preferenceKey = preference.getKey();
338 if (preferenceKey.contains(WIFI_SETTINGS))
341 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
344 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));