1 package net.osdn.gokigen.pkremote.preference.sony;
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.sony.cameraproperty.SonyCameraApiListViewer;
23 import net.osdn.gokigen.pkremote.camera.vendor.sony.operation.CameraPowerOffSony;
24 import net.osdn.gokigen.pkremote.logcat.LogCatViewer;
25 import net.osdn.gokigen.pkremote.preference.IPreferencePropertyAccessor;
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 SonyPreferenceFragment 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 CameraPowerOffSony powerOffController = null;
42 private LogCatViewer logCatViewer = null;
43 private SonyCameraApiListViewer cameraApiListViewer = null;
49 //public static SonyPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IInterfaceProvider factory, @NonNull IChangeScene changeScene)
50 public static SonyPreferenceFragment newInstance(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
52 SonyPreferenceFragment instance = new SonyPreferenceFragment();
53 instance.prepare(context, changeScene);
55 // パラメータはBundleにまとめておく
56 Bundle arguments = new Bundle();
57 //arguments.putString("title", title);
58 //arguments.putString("message", message);
59 instance.setArguments(arguments);
68 private void prepare(@NonNull AppCompatActivity context, @NonNull IChangeScene changeScene)
72 powerOffController = new CameraPowerOffSony(context, changeScene);
73 powerOffController.prepare();
75 logCatViewer = new LogCatViewer(changeScene);
76 logCatViewer.prepare();
78 cameraApiListViewer = new SonyCameraApiListViewer(changeScene);
79 cameraApiListViewer.prepare();
81 this.context = context;
94 public void onAttach(Context activity)
96 super.onAttach(activity);
97 Log.v(TAG, "onAttach()");
102 preferences = PreferenceManager.getDefaultSharedPreferences(activity);
104 // Preference を初期設定する
105 initializePreferences();
107 preferences.registerOnSharedPreferenceChangeListener(this);
119 private void initializePreferences()
123 Map<String, ?> items = preferences.getAll();
124 SharedPreferences.Editor editor = preferences.edit();
126 if (!items.containsKey(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA))
128 editor.putBoolean(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, true);
130 if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
132 editor.putBoolean(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, true);
134 if (!items.containsKey(IPreferencePropertyAccessor.CONNECTION_METHOD))
136 editor.putString(IPreferencePropertyAccessor.CONNECTION_METHOD, IPreferencePropertyAccessor.CONNECTION_METHOD_DEFAULT_VALUE);
138 if (!items.containsKey(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA))
140 editor.putBoolean(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, false);
142 if (!items.containsKey(IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE))
144 editor.putBoolean(IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE, false);
146 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_HOST_IP))
148 editor.putString(IPreferencePropertyAccessor.PIXPRO_HOST_IP, IPreferencePropertyAccessor.PIXPRO_HOST_IP_DEFAULT_VALUE);
150 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT))
152 editor.putString(IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT, IPreferencePropertyAccessor.PIXPRO_COMMAND_PORT_DEFAULT_VALUE);
154 if (!items.containsKey(IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT))
156 editor.putString(IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT, IPreferencePropertyAccessor.PIXPRO_GET_PICS_LIST_TIMEOUT_DEFAULT_VALUE);
158 if (!items.containsKey(IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE))
160 editor.putString(IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE, IPreferencePropertyAccessor.THUMBNAIL_IMAGE_CACHE_SIZE_DEFAULT_VALUE);
162 if (!items.containsKey(IPreferencePropertyAccessor.CANON_HOST_IP)) {
163 editor.putString(IPreferencePropertyAccessor.CANON_HOST_IP, IPreferencePropertyAccessor.CANON_HOST_IP_DEFAULT_VALUE);
165 if (!items.containsKey(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE)) {
166 editor.putString(IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE, IPreferencePropertyAccessor.CANON_CONNECTION_SEQUENCE_DEFAULT_VALUE);
181 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
183 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
189 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
190 value = preferences.getBoolean(key, true);
191 Log.v(TAG, " " + key + " , " + value);
194 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
195 value = preferences.getBoolean(key, true);
196 Log.v(TAG, " " + key + " , " + value);
199 case IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA:
200 value = preferences.getBoolean(key, false);
201 Log.v(TAG, " " + key + " , " + value);
204 case IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE:
205 value = preferences.getBoolean(key, false);
206 Log.v(TAG, " " + key + " , " + value);
210 String strValue = preferences.getString(key, "");
211 setListPreference(key, key, strValue);
222 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
224 Log.v(TAG, "onCreatePreferences()");
227 //super.onCreate(savedInstanceState);
228 addPreferencesFromResource(R.xml.preferences_sony);
230 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
231 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
233 public boolean onPreferenceChange(Preference preference, Object newValue) {
234 preference.setSummary(newValue + " ");
238 connectionMethod.setSummary(connectionMethod.getValue() + " ");
240 findPreference(EXIT_APPLICATION).setOnPreferenceClickListener(powerOffController);
241 findPreference(DEBUG_INFO).setOnPreferenceClickListener(logCatViewer);
242 findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
243 findPreference("sony_api_list").setOnPreferenceClickListener(cameraApiListViewer);
256 public void onResume()
259 Log.v(TAG, "onResume() Start");
263 synchronizedProperty();
270 Log.v(TAG, "onResume() End");
278 public void onPause()
281 Log.v(TAG, "onPause() Start");
285 // Preference変更のリスナを解除
286 preferences.unregisterOnSharedPreferenceChangeListener(this);
293 Log.v(TAG, "onPause() End");
297 * ListPreference の表示データを設定
299 * @param pref_key Preference(表示)のキー
300 * @param key Preference(データ)のキー
301 * @param defaultValue Preferenceのデフォルト値
303 private void setListPreference(String pref_key, String key, String defaultValue)
308 pref = (ListPreference) findPreference(pref_key);
309 String value = preferences.getString(key, defaultValue);
312 pref.setValue(value);
313 pref.setSummary(value);
323 * BooleanPreference の表示データを設定
325 * @param pref_key Preference(表示)のキー
326 * @param key Preference(データ)のキー
327 * @param defaultValue Preferenceのデフォルト値
329 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
333 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
335 boolean value = preferences.getBoolean(key, defaultValue);
336 pref.setChecked(value);
349 private void synchronizedProperty()
351 final FragmentActivity activity = getActivity();
352 final boolean defaultValue = true;
353 if (activity != null)
355 activity.runOnUiThread(new Runnable() {
360 // Preferenceの画面に反映させる
361 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, defaultValue);
362 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, defaultValue);
363 setBooleanPreference(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, false);
364 setBooleanPreference(IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE, IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE, false);
376 public boolean onPreferenceClick(Preference preference)
380 String preferenceKey = preference.getKey();
381 if (preferenceKey.contains(WIFI_SETTINGS))
384 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
387 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));