/*
* Kontalk Android client
* Copyright (C) 2017 Kontalk Devteam <devteam@kontalk.org>
* 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.
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kontalk.ui.prefs;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.widget.Toast;
import org.kontalk.R;
import org.kontalk.util.Preferences;
import java.io.File;
/**
* Appearance settings fragment.
*/
public class AppearanceFragment extends RootPreferenceFragment {
private static final int REQUEST_PICK_BACKGROUND = Activity.RESULT_FIRST_USER + 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_appearance);
// use custom background
final Preference customBg = findPreference("pref_custom_background");
customBg.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
// discard reference to custom background drawable
Preferences.setCachedCustomBackground(null);
return false;
}
});
// set background
final Preference setBackground = findPreference("pref_background_uri");
setBackground.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
try {
final Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(i, REQUEST_PICK_BACKGROUND);
}
catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), R.string.chooser_error_no_gallery_app,
Toast.LENGTH_LONG).show();
}
return true;
}
});
// set balloon theme
final Preference balloons = findPreference("pref_balloons");
balloons.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Preferences.setCachedBalloonTheme((String) newValue);
return true;
}
});
// set balloon theme for groups
final Preference balloonsGroups = findPreference("pref_balloons_groups");
balloonsGroups.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Preferences.setCachedBalloonGroupsTheme((String) newValue);
return true;
}
});
}
@Override
public void onResume() {
super.onResume();
((PreferencesActivity) getActivity()).getSupportActionBar()
.setTitle(R.string.pref_appearance_settings);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_PICK_BACKGROUND) {
if (resultCode == Activity.RESULT_OK) {
Context ctx = getActivity();
if (ctx != null) {
// invalidate any previous reference
Preferences.setCachedCustomBackground(null);
// resize and cache image
// TODO do this in background (might take some time)
try {
File image = Preferences.cacheConversationBackground(ctx, data.getData());
// save to preferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
prefs.edit()
.putString("pref_background_uri", Uri.fromFile(image).toString())
.commit();
} catch (Exception e) {
Toast.makeText(ctx, R.string.err_custom_background,
Toast.LENGTH_LONG).show();
}
}
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}