Skip to content

Instantly share code, notes, and snippets.

@acappelli
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acappelli/87bcd7ba570a59a55042 to your computer and use it in GitHub Desktop.
Save acappelli/87bcd7ba570a59a55042 to your computer and use it in GitHub Desktop.
/*
* Kontalk Android client
* Copyright (C) 2014 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;
import org.kontalk.Kontalk;
import org.kontalk.R;
import org.kontalk.authenticator.Authenticator;
import org.kontalk.client.ServerList;
import org.kontalk.service.MessageCenterService;
import org.kontalk.service.ServerListUpdater;
import org.kontalk.util.Preferences;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gcm.GCMRegistrar;
/**
* Preferences activity.
* TODO convert to fragments layout
* @author Daniele Ricci
*/
public final class PreferencesActivity extends PreferenceActivity {
private static final String TAG = Kontalk.TAG;
private static final int REQUEST_PICK_BACKGROUND = Activity.RESULT_FIRST_USER + 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// no account - redirect to bootstrap preferences
if (Authenticator.getDefaultAccount(this) == null) {
startActivity(new Intent(this, BootstrapPreferences.class));
finish();
return;
}
addPreferencesFromResource(R.xml.preferences);
setupActivity();
// push notifications checkbox
final Preference pushNotifications = findPreference("pref_push_notifications");
pushNotifications.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
CheckBoxPreference pref = (CheckBoxPreference) preference;
if (pref.isChecked())
MessageCenterService.enablePushNotifications(getApplicationContext());
else
MessageCenterService.disablePushNotifications(getApplicationContext());
return true;
}
});
// message center restart
final Preference restartMsgCenter = findPreference("pref_restart_msgcenter");
restartMsgCenter.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Log.w(TAG, "manual message center restart requested");
MessageCenterService.restart(getApplicationContext());
Toast.makeText(PreferencesActivity.this, R.string.msg_msgcenter_restarted, Toast.LENGTH_SHORT).show();
return true;
}
});
// regenerate key pair
final Preference regenKeyPair = findPreference("pref_regenerate_keypair");
regenKeyPair.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(PreferencesActivity.this, R.string.msg_generating_keypair,
Toast.LENGTH_LONG).show();
MessageCenterService.regenerateKeyPair(getApplicationContext());
return true;
}
});
// export key pair
final Preference exportKeyPair = findPreference("pref_export_keypair");
exportKeyPair.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
// TODO check for external storage presence
try {
((Kontalk)getApplicationContext()).exportPersonalKey();
Toast.makeText(PreferencesActivity.this,
R.string.msg_keypair_exported,
Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(PreferencesActivity.this,
// TODO i18n
"Unable to export personal key.",
Toast.LENGTH_LONG).show();
}
return true;
}
});
// use custom background
final Preference customBg = findPreference("pref_custom_background");
customBg.setOnPreferenceClickListener(new 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 OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
final Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(i, REQUEST_PICK_BACKGROUND);
return true;
}
});
// set balloon theme
final Preference balloons = findPreference("pref_balloons");
balloons.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Preferences.setCachedBalloonTheme((String) newValue);
return true;
}
});
// set audio type
final Preference audioFormat = findPreference("pref_audio");
audioFormat.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Preferences.setAudioFormat(getApplicationContext(),(String) newValue);
return true;
}
});
// disable push notifications if GCM is not available on the device
try {
GCMRegistrar.checkDevice(this);
}
catch (UnsupportedOperationException unsupported) {
final Preference push = findPreference("pref_push_notifications");
push.setEnabled(false);
}
// manual server address is handled in Application context
// server list last update timestamp
final Preference updateServerList = findPreference("pref_update_server_list");
updateServerList.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
final ServerListUpdater updater = new ServerListUpdater(PreferencesActivity.this);
final ProgressDialog diag = new ProgressDialog(PreferencesActivity.this);
diag.setCancelable(true);
diag.setMessage(getString(R.string.serverlist_updating));
diag.setIndeterminate(true);
diag.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
updater.cancel();
}
});
updater.setListener(new ServerListUpdater.UpdaterListener() {
@Override
public void error(Throwable e) {
diag.cancel();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(PreferencesActivity.this, R.string.serverlist_update_error,
Toast.LENGTH_LONG).show();
}
});
}
@Override
public void nodata() {
diag.cancel();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(PreferencesActivity.this, R.string.serverlist_update_nodata,
Toast.LENGTH_LONG).show();
}
});
}
@Override
public void updated(final ServerList list) {
diag.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
Preferences.updateServerListLastUpdate(updateServerList, list);
// restart message center
MessageCenterService.restart(getApplicationContext());
}
});
}
});
diag.show();
updater.start();
return true;
}
});
// update 'last update' string
ServerList list = ServerListUpdater.getCurrentList(this);
if (list != null)
Preferences.updateServerListLastUpdate(updateServerList, list);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActivity() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar bar = getActionBar();
bar.setDisplayShowHomeEnabled(true);
bar.setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
startActivity(new Intent(this, ConversationList.class));
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_PICK_BACKGROUND) {
if (resultCode == RESULT_OK) {
// invalidate any previous reference
Preferences.setCachedCustomBackground(null);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit()
.putString("pref_background_uri", data.getDataString())
.commit();
}
}
else
super.onActivityResult(requestCode, resultCode, data);
}
public static void start(Activity context) {
Intent intent = new Intent(context, PreferencesActivity.class);
context.startActivityIfNeeded(intent, -1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment