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(@NonNull 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))
122 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
124 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
126 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
128 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD))
130 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
132 if (!items.containsKey(IPreferencePropertyAccessor.CANON_RAW_SUFFIX))
134 editor.putString(IPreferencePropertyAccessor.CANON_RAW_SUFFIX, IPreferencePropertyAccessor.CANON_RAW_SUFFIX_DEFAULT_VALUE);
136 if (!items.containsKey(IPreferencePropertyAccessor.CANON_RECEIVE_WAIT))
138 editor.putString(IPreferencePropertyAccessor.CANON_RECEIVE_WAIT, IPreferencePropertyAccessor.CANON_RECEIVE_WAIT_DEFAULT_VALUE);
140 if (!items.containsKey(IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL))
142 editor.putBoolean(IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL, false);
144 if (!items.containsKey(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA))
146 editor.putBoolean(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, false);
148 if (!items.containsKey(IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE))
150 editor.putBoolean(IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE, false);
152 if (!items.containsKey(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT))
154 editor.putString(IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.RICOH_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
156 if (!items.containsKey(IPreferencePropertyAccessor.USE_OSC_THETA_V21))
158 editor.putBoolean(IPreferencePropertyAccessor.USE_OSC_THETA_V21, false);
160 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_HOST_IP))
162 editor.putString(IPreferencePropertyAccessor.PIXPRO_HOST_IP, IPreferencePropertyAccessor.PIXPRO_HOST_IP_DEFAULT_VALUE);
164 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT))
166 editor.putString(IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT, IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT_DEFAULT_VALUE);
168 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT))
170 editor.putString(IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
172 if (!items.containsKey(IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE))
174 editor.putString(IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE, IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE_DEFAULT_VALUE);
176 if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
177 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
179 if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
180 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
195 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
197 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
203 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
204 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
205 value = preferences.getBoolean(key, true);
206 Log.v(TAG, " " + key + " , " + value);
209 case IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL:
210 value = preferences.getBoolean(key, false);
211 Log.v(TAG, " " + key + " , " + value);
215 String strValue = preferences.getString(key, "");
216 setListPreference(key, key, strValue);
227 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
229 Log.v(TAG, "onCreatePreferences()");
232 //super.onCreate(savedInstanceState);
233 addPreferencesFromResource(R.xml.preferences_canon);
235 ListPreference connectionMethod = findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
236 if (connectionMethod != null)
238 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
240 public boolean onPreferenceChange(Preference preference, Object newValue) {
241 preference.setSummary(newValue + " ");
245 connectionMethod.setSummary(connectionMethod.getValue() + " ");
247 findPreference(EXIT_APPLICATION).setOnPreferenceClickListener(powerOffController);
248 findPreference(DEBUG_INFO).setOnPreferenceClickListener(logCatViewer);
249 findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
262 public void onResume()
265 Log.v(TAG, "onResume() Start");
268 synchronizedProperty();
274 Log.v(TAG, "onResume() End");
282 public void onPause()
285 Log.v(TAG, "onPause() Start");
289 // Preference変更のリスナを解除
290 preferences.unregisterOnSharedPreferenceChangeListener(this);
297 Log.v(TAG, "onPause() End");
301 * ListPreference の表示データを設定
303 * @param pref_key Preference(表示)のキー
304 * @param key Preference(データ)のキー
305 * @param defaultValue Preferenceのデフォルト値
307 private void setListPreference(String pref_key, String key, String defaultValue)
311 ListPreference pref = findPreference(pref_key);
312 String value = preferences.getString(key, defaultValue);
315 pref.setValue(value);
316 pref.setSummary(value);
326 * BooleanPreference の表示データを設定
328 * @param pref_key Preference(表示)のキー
329 * @param key Preference(データ)のキー
330 * @param defaultValue Preferenceのデフォルト値
332 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
336 CheckBoxPreference pref = findPreference(pref_key);
338 boolean value = preferences.getBoolean(key, defaultValue);
339 pref.setChecked(value);
352 private void synchronizedProperty()
354 final FragmentActivity activity = getActivity();
355 if (activity != null)
357 activity.runOnUiThread(new Runnable() {
362 // Preferenceの画面に反映させる
363 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
364 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
365 setBooleanPreference(IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL, IPreferencePropertyAccessor.CANON_USE_SCREENNAIL_AS_SMALL, false);
377 public boolean onPreferenceClick(Preference preference)
381 String preferenceKey = preference.getKey();
382 if (preferenceKey.contains(WIFI_SETTINGS))
385 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
388 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));