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);
175 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
177 Log.v(TAG, "onSharedPreferenceChanged() : " + key);
183 case IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA:
184 value = preferences.getBoolean(key, true);
185 Log.v(TAG, " " + key + " , " + value);
188 case IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW:
189 value = preferences.getBoolean(key, true);
190 Log.v(TAG, " " + key + " , " + value);
193 case IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA:
194 value = preferences.getBoolean(key, false);
195 Log.v(TAG, " " + key + " , " + value);
198 case IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE:
199 value = preferences.getBoolean(key, false);
200 Log.v(TAG, " " + key + " , " + value);
204 String strValue = preferences.getString(key, "");
205 setListPreference(key, key, strValue);
216 public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
218 Log.v(TAG, "onCreatePreferences()");
221 //super.onCreate(savedInstanceState);
222 addPreferencesFromResource(R.xml.preferences_sony);
224 ListPreference connectionMethod = (ListPreference) findPreference(IPreferencePropertyAccessor.CONNECTION_METHOD);
225 connectionMethod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
227 public boolean onPreferenceChange(Preference preference, Object newValue) {
228 preference.setSummary(newValue + " ");
232 connectionMethod.setSummary(connectionMethod.getValue() + " ");
234 findPreference(EXIT_APPLICATION).setOnPreferenceClickListener(powerOffController);
235 findPreference(DEBUG_INFO).setOnPreferenceClickListener(logCatViewer);
236 findPreference(WIFI_SETTINGS).setOnPreferenceClickListener(this);
237 findPreference("sony_api_list").setOnPreferenceClickListener(cameraApiListViewer);
250 public void onResume()
253 Log.v(TAG, "onResume() Start");
257 synchronizedProperty();
264 Log.v(TAG, "onResume() End");
272 public void onPause()
275 Log.v(TAG, "onPause() Start");
279 // Preference変更のリスナを解除
280 preferences.unregisterOnSharedPreferenceChangeListener(this);
287 Log.v(TAG, "onPause() End");
291 * ListPreference の表示データを設定
293 * @param pref_key Preference(表示)のキー
294 * @param key Preference(データ)のキー
295 * @param defaultValue Preferenceのデフォルト値
297 private void setListPreference(String pref_key, String key, String defaultValue)
302 pref = (ListPreference) findPreference(pref_key);
303 String value = preferences.getString(key, defaultValue);
306 pref.setValue(value);
307 pref.setSummary(value);
317 * BooleanPreference の表示データを設定
319 * @param pref_key Preference(表示)のキー
320 * @param key Preference(データ)のキー
321 * @param defaultValue Preferenceのデフォルト値
323 private void setBooleanPreference(String pref_key, String key, boolean defaultValue)
327 CheckBoxPreference pref = (CheckBoxPreference) findPreference(pref_key);
329 boolean value = preferences.getBoolean(key, defaultValue);
330 pref.setChecked(value);
343 private void synchronizedProperty()
345 final FragmentActivity activity = getActivity();
346 final boolean defaultValue = true;
347 if (activity != null)
349 activity.runOnUiThread(new Runnable() {
354 // Preferenceの画面に反映させる
355 setBooleanPreference(IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, IPreferencePropertyAccessor.AUTO_CONNECT_TO_CAMERA, defaultValue);
356 setBooleanPreference(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW, defaultValue);
357 setBooleanPreference(IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, IPreferencePropertyAccessor.GET_SMALL_PICTURE_AS_VGA, false);
358 setBooleanPreference(IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE, IPreferencePropertyAccessor.USE_SMARTPHONE_TRANSFER_MODE, false);
370 public boolean onPreferenceClick(Preference preference)
374 String preferenceKey = preference.getKey();
375 if (preferenceKey.contains(WIFI_SETTINGS))
378 Log.v(TAG, " onPreferenceClick : " + preferenceKey);
381 context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));