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.preference.PreferencePropertyInitializer;
26 import net.osdn.gokigen.pkremote.scene.IChangeScene;
28 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.DEBUG_INFO;
29 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.EXIT_APPLICATION;
30 import static net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor.WIFI_SETTINGS;
36 public class CanonPreferenceFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener
38 private final String TAG = toString();
39 private AppCompatActivity context = null;
40 private SharedPreferences preferences = null;
41 private PtpIpCameraPowerOff powerOffController = null;
42 private LogCatViewer logCatViewer = null;
48 public static CanonPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
50 CanonPreferenceFragment instance = new CanonPreferenceFragment();
51 instance.prepare(context, changeScene);
53 // パラメータはBundleにまとめておく
54 Bundle arguments = new Bundle();
55 //arguments.putString("title", title);
56 //arguments.putString("message", message);
57 instance.setArguments(arguments);
66 private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
70 powerOffController = new PtpIpCameraPowerOff(context, changeScene);
71 powerOffController.prepare();
73 logCatViewer = new LogCatViewer(changeScene);
74 logCatViewer.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 PreferencePropertyInitializer initializer = new PreferencePropertyInitializer(activity);
101 initializer.initializePreferences();
103 preferences.registerOnSharedPreferenceChangeListener(this);
116 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
118 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
123 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW -> {
124 value = preferences.getBoolean(key, true);
125 Log.v(TAG, " " + key + " , " + value);
127 case IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL -> {
128 value = preferences.getBoolean(key, false);
129 Log.v(TAG, " " + key + " , " + value);
132 String strValue = preferences.getString(key, "");
133 setListPreference(key, key, strValue);
144 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
146 Log.v(TAG, "onCreatePreferences()");
149 //super.onCreate(savedInstanceState);
150 addPreferencesFromResource(R.xml.preferences_canon);
152 ListPreference connectionMethod = findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
153 if (connectionMethod != null)
155 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
157 public boolean onPreferenceChange(Preference preference, Object newValue) {
158 preference.setSummary(newValue + " ");
162 connectionMethod.setSummary(connectionMethod.getValue() + " ");
164 findPreference(EXIT_APPLICATION).setOnPreferenceClickListener(powerOffController);
165 findPreference(DEBUG_INFO).setOnPreferenceClickListener(logCatViewer);
166 findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
179 public void onResume()
182 Log.v(TAG, "onResume() Start");
185 synchronizedProperty();
191 Log.v(TAG, "onResume() End");
199 public void onPause()
202 Log.v(TAG, "onPause() Start");
206 // Preference変更のリスナを解除
207 preferences.unregisterOnSharedPreferenceChangeListener(this);
214 Log.v(TAG, "onPause() End");
218 * ListPreference の表示データを設定
220 * @param pref_key Preference(表示)のキー
221 * @param key Preference(データ)のキー
222 * @param defaultValue Preferenceのデフォルト値
224 private void setListPreference(String pref_key, String key, String defaultValue)
228 ListPreference pref = findPreference(pref_key);
229 String value = preferences.getString(key, defaultValue);
232 pref.setValue(value);
233 pref.setSummary(value);
243 * BooleanPreference の表示データを設定
245 * @param pref_key Preference(表示)のキー
246 * @param key Preference(データ)のキー
247 * @param defaultValue Preferenceのデフォルト値
249 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
253 CheckBoxPreference pref = findPreference(pref_key);
255 boolean value = preferences.getBoolean(key, defaultValue);
256 pref.setChecked(value);
269 private void synchronizedProperty()
271 final FragmentActivity activity = getActivity();
272 if (activity != null)
274 activity.runOnUiThread(new Runnable() {
279 // Preferenceの画面に反映させる
280 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
281 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
282 setBooleanPreference(IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL, IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL, false);
294 public boolean onPreferenceClick(Preference preference)
298 String preferenceKey = preference.getKey();
299 if (preferenceKey.contains(WIFI_SETTINGS))
302 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
305 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));