package fr.gouv.etalab.mastodon.fragments; /* Copyright 2017 Thomas Schneider * * This file is a part of Mastalab * * This program is free software; you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation; either version 3 of the * License, or (at your option) any later version. * * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * Public License for more details. * * You should have received a copy of the GNU General Public License along with Mastalab; if not, * see . */ import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.support.v7.app.AlertDialog; import android.content.ContentUris; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; import android.support.v4.app.Fragment; import android.support.v7.widget.SwitchCompat; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.sqlite.AccountDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; import mastodon.etalab.gouv.fr.mastodon.R; import static android.app.Activity.RESULT_OK; import static fr.gouv.etalab.mastodon.helper.Helper.CHANGE_THEME_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION; import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; /** * Created by Thomas on 24/06/2017. * Fragment for settings, yes I didn't use PreferenceFragment :) */ public class SettingsFragment extends Fragment { private Context context; private static final int ACTIVITY_CHOOSE_FILE = 411; private TextView set_folder; int count = 0; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.fragment_settings, container, false); context = getContext(); final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); boolean auto_store = sharedpreferences.getBoolean(Helper.SET_AUTO_STORE, true); final CheckBox set_auto_store = (CheckBox) rootView.findViewById(R.id.set_auto_store); set_auto_store.setChecked(auto_store); set_auto_store.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean(Helper.SET_AUTO_STORE, set_auto_store.isChecked()); editor.apply(); } }); boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true); final CheckBox set_show_error_messages = (CheckBox) rootView.findViewById(R.id.set_show_error_messages); set_show_error_messages.setChecked(show_error_messages); set_show_error_messages.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean(Helper.SET_SHOW_ERROR_MESSAGES, set_show_error_messages.isChecked()); editor.apply(); } }); boolean show_media_urls = sharedpreferences.getBoolean(Helper.SET_MEDIA_URLS, true); final CheckBox set_auto_add_media_url = (CheckBox) rootView.findViewById(R.id.set_auto_add_media_url); set_auto_add_media_url.setChecked(show_media_urls); set_auto_add_media_url.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean(Helper.SET_MEDIA_URLS, set_auto_add_media_url.isChecked()); editor.apply(); } }); boolean notif_validation = sharedpreferences.getBoolean(Helper.SET_NOTIF_VALIDATION, true); final CheckBox set_share_validation = (CheckBox) rootView.findViewById(R.id.set_share_validation); set_share_validation.setChecked(notif_validation); set_share_validation.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean(Helper.SET_NOTIF_VALIDATION, set_share_validation.isChecked()); editor.apply(); } }); final CheckBox set_embedded_browser = (CheckBox) rootView.findViewById(R.id.set_embedded_browser); final LinearLayout set_javascript_container = (LinearLayout) rootView.findViewById(R.id.set_javascript_container); final SwitchCompat set_javascript = (SwitchCompat) rootView.findViewById(R.id.set_javascript); boolean javascript = sharedpreferences.getBoolean(Helper.SET_JAVASCRIPT, true); boolean embedded_browser = sharedpreferences.getBoolean(Helper.SET_EMBEDDED_BROWSER, true); if( !embedded_browser){ set_javascript_container.setVisibility(View.GONE); }else{ set_javascript_container.setVisibility(View.VISIBLE); } set_embedded_browser.setChecked(embedded_browser); set_embedded_browser.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean(Helper.SET_EMBEDDED_BROWSER, set_embedded_browser.isChecked()); editor.apply(); if( !set_embedded_browser.isChecked()){ set_javascript_container.setVisibility(View.GONE); }else{ set_javascript_container.setVisibility(View.VISIBLE); } } }); set_javascript.setChecked(javascript); set_javascript.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean(Helper.SET_JAVASCRIPT, isChecked); editor.apply(); } }); final LinearLayout set_cookies_container = (LinearLayout) rootView.findViewById(R.id.set_cookies_container); final SwitchCompat set_cookies = (SwitchCompat) rootView.findViewById(R.id.set_cookies); boolean cookies = sharedpreferences.getBoolean(Helper.SET_COOKIES, false); set_cookies.setChecked(cookies); set_cookies.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean(Helper.SET_COOKIES, isChecked); editor.apply(); } }); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { set_cookies_container.setVisibility(View.VISIBLE); }else { set_cookies_container.setVisibility(View.GONE); } final String targeted_folder = sharedpreferences.getString(Helper.SET_FOLDER_RECORD, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()); set_folder = (TextView) rootView.findViewById(R.id.set_folder); set_folder.setText(targeted_folder); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { set_folder.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @SuppressLint("InlinedApi") Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, ACTIVITY_CHOOSE_FILE); } }); }else { LinearLayout file_chooser = (LinearLayout) rootView.findViewById(R.id.file_chooser); file_chooser.setVisibility(View.GONE); } final SwitchCompat set_night_mode = (SwitchCompat) rootView.findViewById(R.id.set_night_mode); set_night_mode.setChecked(theme == Helper.THEME_DARK); set_night_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putInt(Helper.SET_THEME, isChecked?Helper.THEME_DARK:Helper.THEME_LIGHT); editor.apply(); if( isChecked){ getActivity().setTheme(R.style.AppThemeDark); }else { getActivity().setTheme(R.style.AppTheme); } getActivity().recreate(); Intent intent = new Intent(context, MainActivity.class); intent.putExtra(INTENT_ACTION, CHANGE_THEME_INTENT); startActivity(intent); } }); // NSFW Timeout SeekBar nsfwTimeoutSeekBar = (SeekBar) rootView.findViewById(R.id.set_nsfw_timeout); final TextView set_nsfw_timeout_value = (TextView) rootView.findViewById(R.id.set_nsfw_timeout_value); nsfwTimeoutSeekBar.setMax(30); int nsfwTimeout = sharedpreferences.getInt(Helper.SET_NSFW_TIMEOUT, 5); nsfwTimeoutSeekBar.setProgress(nsfwTimeout); set_nsfw_timeout_value.setText(String.valueOf(nsfwTimeout)); nsfwTimeoutSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) {} @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { set_nsfw_timeout_value.setText(String.valueOf(progress)); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putInt(Helper.SET_NSFW_TIMEOUT, progress); editor.apply(); } }); LinearLayout toot_visibility_container = (LinearLayout) rootView.findViewById(R.id.toot_visibility_container); String prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); final Account account = new AccountDAO(context, db).getAccountByToken(prefKeyOauthTokenT); final ImageView set_toot_visibility = (ImageView) rootView.findViewById(R.id.set_toot_visibility); //Only displayed for non locked accounts if (account != null && !account.isLocked()) { String tootVisibility = sharedpreferences.getString(Helper.SET_TOOT_VISIBILITY + "@" + account.getAcct() + "@" + account.getInstance(), "public"); switch (tootVisibility) { case "public": set_toot_visibility.setImageResource(R.drawable.ic_action_globe); break; case "unlisted": set_toot_visibility.setImageResource(R.drawable.ic_action_lock_open); break; case "private": set_toot_visibility.setImageResource(R.drawable.ic_action_lock_closed); break; case "direct": set_toot_visibility.setImageResource(R.drawable.ic_local_post_office); break; } changeColor(); }else { toot_visibility_container.setVisibility(View.GONE); } set_toot_visibility.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder dialog = new AlertDialog.Builder(context); dialog.setTitle(R.string.toot_visibility_tilte); final String[] stringArray = getResources().getStringArray(R.array.toot_visibility); final ArrayAdapter arrayAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, stringArray); dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int position) { dialog.dismiss(); } }); dialog.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int position) { String visibility = "public"; switch (position){ case 0: visibility = "public"; set_toot_visibility.setImageResource(R.drawable.ic_action_globe); break; case 1: visibility = "unlisted"; set_toot_visibility.setImageResource(R.drawable.ic_action_lock_open); break; case 2: visibility = "private"; set_toot_visibility.setImageResource(R.drawable.ic_action_lock_closed); break; case 3: visibility = "direct"; set_toot_visibility.setImageResource(R.drawable.ic_local_post_office); break; } if( account != null) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Helper.SET_TOOT_VISIBILITY + "@" + account.getAcct() + "@" + account.getInstance(), visibility); editor.apply(); Toast.makeText(context, context.getString(R.string.toast_visibility_changed, "@" + account.getAcct() + "@" + account.getInstance()), Toast.LENGTH_SHORT).show(); }else { Toast.makeText(context, R.string.toast_error,Toast.LENGTH_SHORT).show(); } changeColor(); dialog.dismiss(); } }); dialog.show(); } }); final Spinner tabs_layout_spinner = (Spinner) rootView.findViewById(R.id.tabs_layout_spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.settings_menu_tabs, android.R.layout.simple_spinner_item); tabs_layout_spinner.setAdapter(adapter); int positionSpinner = (sharedpreferences.getInt(Helper.SET_TABS, Helper.THEME_TABS) - 1); tabs_layout_spinner.setSelection(positionSpinner); tabs_layout_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { if( count > 0){ SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putInt(Helper.SET_TABS, (position + 1)); editor.apply(); Helper.switchLayout(getActivity()); getActivity().recreate(); Intent intent = new Intent(context, MainActivity.class); intent.putExtra(INTENT_ACTION, CHANGE_THEME_INTENT); startActivity(intent); }else { count++; } } @Override public void onNothingSelected(AdapterView parent) { } }); return rootView; } private void changeColor(){ SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); if( theme == Helper.THEME_DARK){ changeDrawableColor(context, R.drawable.ic_action_globe,R.color.dark_text); changeDrawableColor(context, R.drawable.ic_action_lock_open,R.color.dark_text); changeDrawableColor(context, R.drawable.ic_action_lock_closed,R.color.dark_text); changeDrawableColor(context, R.drawable.ic_local_post_office,R.color.dark_text); }else { changeDrawableColor(context, R.drawable.ic_action_globe,R.color.white); changeDrawableColor(context, R.drawable.ic_action_lock_open,R.color.white); changeDrawableColor(context, R.drawable.ic_action_lock_closed,R.color.white); changeDrawableColor(context, R.drawable.ic_local_post_office,R.color.white); } } @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) return; if(requestCode == ACTIVITY_CHOOSE_FILE) { Uri treeUri = data.getData(); Uri docUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri)); String path = getPath(context, docUri); if( path == null ) path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(); final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Helper.SET_FOLDER_RECORD, path); editor.apply(); set_folder.setText(path); } } @Override public void onCreate(Bundle saveInstance) { super.onCreate(saveInstance); } @Override public void onAttach(Context context) { super.onAttach(context); this.context = context; } //From: https://gist.github.com/asifmujteba/d89ba9074bc941de1eaa#file-asfurihelper @TargetApi(Build.VERSION_CODES.KITKAT) public static String getPath(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } } // DownloadsProvider else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { // Return the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null; } /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); } }