SyncedFolderPreferencesDialogFragment.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * Copyright (C) 2016 Andy Scherzinger
  6. * Copyright (C) 2016 Nextcloud
  7. * <p>
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. * <p>
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. * <p>
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.dialog;
  22. import android.app.Activity;
  23. import android.app.Dialog;
  24. import android.content.DialogInterface;
  25. import android.content.Intent;
  26. import android.graphics.Typeface;
  27. import android.os.Bundle;
  28. import android.support.annotation.NonNull;
  29. import android.support.v4.app.DialogFragment;
  30. import android.support.v7.app.AlertDialog;
  31. import android.support.v7.widget.SwitchCompat;
  32. import android.text.style.StyleSpan;
  33. import android.view.LayoutInflater;
  34. import android.view.View;
  35. import android.view.View.OnClickListener;
  36. import android.view.ViewGroup;
  37. import android.widget.CheckBox;
  38. import android.widget.TextView;
  39. import com.owncloud.android.R;
  40. import com.owncloud.android.datamodel.SyncedFolderDisplayItem;
  41. import com.owncloud.android.lib.common.utils.Log_OC;
  42. import com.owncloud.android.ui.activity.FolderPickerActivity;
  43. import com.owncloud.android.ui.dialog.parcel.SyncedFolderParcelable;
  44. import com.owncloud.android.utils.DisplayUtils;
  45. /**
  46. * Dialog to show the preferences/configuration of a synced folder allowing the user to change the different parameters.
  47. */
  48. public class SyncedFolderPreferencesDialogFragment extends DialogFragment {
  49. private final static String TAG = SyncedFolderPreferencesDialogFragment.class.getSimpleName();
  50. public static final String SYNCED_FOLDER_PARCELABLE = "SyncedFolderParcelable";
  51. public static final int REQUEST_CODE__SELECT_REMOTE_FOLDER = 0;
  52. private CharSequence[] mUploadBehaviorItemStrings;
  53. protected View mView = null;
  54. private SwitchCompat mEnabledSwitch;
  55. private CheckBox mUploadOnWifiCheckbox;
  56. private CheckBox mUploadOnChargingCheckbox;
  57. private CheckBox mUploadUseSubfoldersCheckbox;
  58. private TextView mUploadBehaviorSummary;
  59. private TextView mLocalFolderPath;
  60. private TextView mRemoteFolderSummary;
  61. private SyncedFolderParcelable mSyncedFolder;
  62. public static SyncedFolderPreferencesDialogFragment newInstance(SyncedFolderDisplayItem syncedFolder, int section) {
  63. SyncedFolderPreferencesDialogFragment dialogFragment = new SyncedFolderPreferencesDialogFragment();
  64. if (syncedFolder == null) {
  65. throw new IllegalArgumentException("SyncedFolder is mandatory but NULL!");
  66. }
  67. Bundle args = new Bundle();
  68. args.putParcelable(SYNCED_FOLDER_PARCELABLE, new SyncedFolderParcelable(syncedFolder, section));
  69. dialogFragment.setArguments(args);
  70. dialogFragment.setStyle(STYLE_NORMAL,R.style.Theme_ownCloud_Dialog);
  71. return dialogFragment;
  72. }
  73. @Override
  74. public void onAttach(Activity activity) {
  75. super.onAttach(activity);
  76. if (!(activity instanceof OnSyncedFolderPreferenceListener)) {
  77. throw new IllegalArgumentException("The host activity must implement "
  78. + OnSyncedFolderPreferenceListener.class.getCanonicalName());
  79. }
  80. }
  81. @Override
  82. public void onCreate(Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84. // keep the state of the fragment on configuration changes
  85. setRetainInstance(true);
  86. setCancelable(false);
  87. mView = null;
  88. mSyncedFolder = getArguments().getParcelable(SYNCED_FOLDER_PARCELABLE);
  89. mUploadBehaviorItemStrings = getResources().getTextArray(R.array.pref_behaviour_entries);
  90. }
  91. @Override
  92. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  93. Log_OC.d(TAG, "onCreateView, savedInstanceState is " + savedInstanceState);
  94. mView = inflater.inflate(R.layout.folder_sync_settings_layout, container, false);
  95. setupDialogElements(mView);
  96. setupListeners(mView);
  97. return mView;
  98. }
  99. /**
  100. * find all relevant UI elements and set their values.
  101. *
  102. * @param view the parent view
  103. */
  104. private void setupDialogElements(View view) {
  105. // find/saves UI elements
  106. mEnabledSwitch = (SwitchCompat) view.findViewById(R.id.sync_enabled);
  107. mLocalFolderPath = (TextView) view.findViewById(R.id.folder_sync_settings_local_folder_path);
  108. mRemoteFolderSummary = (TextView) view.findViewById(R.id.remote_folder_summary);
  109. mUploadOnWifiCheckbox = (CheckBox) view.findViewById(R.id.setting_instant_upload_on_wifi_checkbox);
  110. mUploadOnChargingCheckbox = (CheckBox) view.findViewById(R.id.setting_instant_upload_on_charging_checkbox);
  111. mUploadUseSubfoldersCheckbox = (CheckBox) view.findViewById(R.id
  112. .setting_instant_upload_path_use_subfolders_checkbox);
  113. mUploadBehaviorSummary = (TextView) view.findViewById(R.id.setting_instant_behaviour_summary);
  114. // Set values
  115. setEnabled(mSyncedFolder.getEnabled());
  116. mLocalFolderPath.setText(
  117. DisplayUtils.createTextWithSpan(
  118. String.format(
  119. getString(R.string.folder_sync_preferences_folder_path),
  120. mSyncedFolder.getLocalPath()),
  121. mSyncedFolder.getFolderName(),
  122. new StyleSpan(Typeface.BOLD)));
  123. mRemoteFolderSummary.setText(mSyncedFolder.getRemotePath());
  124. mUploadOnWifiCheckbox.setChecked(mSyncedFolder.getWifiOnly());
  125. mUploadOnChargingCheckbox.setChecked(mSyncedFolder.getChargingOnly());
  126. mUploadUseSubfoldersCheckbox.setChecked(mSyncedFolder.getSubfolderByDate());
  127. mUploadBehaviorSummary.setText(mUploadBehaviorItemStrings[mSyncedFolder.getUploadActionInteger()]);
  128. }
  129. /**
  130. * set correct icon/flag.
  131. *
  132. * @param enabled if enabled or disabled
  133. */
  134. private void setEnabled(boolean enabled) {
  135. mSyncedFolder.setEnabled(enabled);
  136. mEnabledSwitch.setChecked(enabled);
  137. }
  138. /**
  139. * set (new) remote path on activity result of the folder picker activity. The result gets originally propagated
  140. * to the underlying activity since the picker is an activity and the result can't get passed to the dialog
  141. * fragment directly.
  142. *
  143. * @param path the remote path to be set
  144. */
  145. public void setRemoteFolderSummary(String path) {
  146. mSyncedFolder.setRemotePath(path);
  147. mRemoteFolderSummary.setText(path);
  148. }
  149. /**
  150. * setup all listeners.
  151. *
  152. * @param view the parent view
  153. */
  154. private void setupListeners(View view) {
  155. view.findViewById(R.id.save).setOnClickListener(new OnSyncedFolderSaveClickListener());
  156. view.findViewById(R.id.cancel).setOnClickListener(new OnSyncedFolderCancelClickListener());
  157. view.findViewById(R.id.setting_instant_upload_on_wifi_container).setOnClickListener(
  158. new OnClickListener() {
  159. @Override
  160. public void onClick(View v) {
  161. mSyncedFolder.setWifiOnly(!mSyncedFolder.getWifiOnly());
  162. mUploadOnWifiCheckbox.toggle();
  163. }
  164. });
  165. view.findViewById(R.id.setting_instant_upload_on_charging_container).setOnClickListener(
  166. new OnClickListener() {
  167. @Override
  168. public void onClick(View v) {
  169. mSyncedFolder.setChargingOnly(!mSyncedFolder.getChargingOnly());
  170. mUploadOnChargingCheckbox.toggle();
  171. }
  172. });
  173. view.findViewById(R.id.setting_instant_upload_path_use_subfolders_container).setOnClickListener(
  174. new OnClickListener() {
  175. @Override
  176. public void onClick(View v) {
  177. mSyncedFolder.setSubfolderByDate(!mSyncedFolder.getSubfolderByDate());
  178. mUploadUseSubfoldersCheckbox.toggle();
  179. }
  180. });
  181. view.findViewById(R.id.remote_folder_container).setOnClickListener(new OnClickListener() {
  182. @Override
  183. public void onClick(View v) {
  184. Intent action = new Intent(getActivity(), FolderPickerActivity.class);
  185. action.putExtra(
  186. FolderPickerActivity.EXTRA_ACTION, getResources().getText(R.string.choose_remote_folder));
  187. getActivity().startActivityForResult(action, REQUEST_CODE__SELECT_REMOTE_FOLDER);
  188. }
  189. });
  190. view.findViewById(R.id.sync_enabled).setOnClickListener(new OnClickListener() {
  191. @Override
  192. public void onClick(View v) {
  193. setEnabled(!mSyncedFolder.getEnabled());
  194. }
  195. });
  196. view.findViewById(R.id.setting_instant_behaviour_container).setOnClickListener(
  197. new OnClickListener() {
  198. @Override
  199. public void onClick(View v) {
  200. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  201. builder.setTitle(R.string.prefs_instant_behaviour_dialogTitle)
  202. .setSingleChoiceItems(getResources().getTextArray(R.array.pref_behaviour_entries),
  203. mSyncedFolder.getUploadActionInteger(),
  204. new
  205. DialogInterface.OnClickListener() {
  206. public void onClick(DialogInterface dialog, int which) {
  207. mSyncedFolder.setUploadAction(
  208. getResources().getTextArray(
  209. R.array.pref_behaviour_entryValues)[which].toString());
  210. mUploadBehaviorSummary.setText(SyncedFolderPreferencesDialogFragment
  211. .this.mUploadBehaviorItemStrings[which]);
  212. dialog.dismiss();
  213. }
  214. });
  215. builder.create().show();
  216. }
  217. });
  218. }
  219. @Override
  220. @NonNull
  221. public Dialog onCreateDialog(Bundle savedInstanceState) {
  222. final Dialog dialog = super.onCreateDialog(savedInstanceState);
  223. dialog.setTitle(null);
  224. return dialog;
  225. }
  226. @Override
  227. public void onDestroyView() {
  228. Log_OC.d(TAG, "destroy SyncedFolderPreferencesDialogFragment view");
  229. if (getDialog() != null && getRetainInstance()) {
  230. getDialog().setDismissMessage(null);
  231. }
  232. super.onDestroyView();
  233. }
  234. private class OnSyncedFolderSaveClickListener implements OnClickListener {
  235. @Override
  236. public void onClick(View v) {
  237. dismiss();
  238. ((OnSyncedFolderPreferenceListener) getActivity()).onSaveSyncedFolderPreference(mSyncedFolder);
  239. }
  240. }
  241. private class OnSyncedFolderCancelClickListener implements OnClickListener {
  242. @Override
  243. public void onClick(View v) {
  244. dismiss();
  245. ((OnSyncedFolderPreferenceListener) getActivity()).onCancelSyncedFolderPreference();
  246. }
  247. }
  248. public interface OnSyncedFolderPreferenceListener {
  249. void onSaveSyncedFolderPreference(SyncedFolderParcelable syncedFolder);
  250. void onCancelSyncedFolderPreference();
  251. }
  252. }