use of android.content.SharedPreferences in project platform_frameworks_base by android.
the class TestAppWidgetProvider method onReceive.
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "intent=" + intent);
if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
Log.d(TAG, "ENABLED");
} else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
Log.d(TAG, "DISABLED");
} else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
Log.d(TAG, "UPDATE");
// BEGIN_INCLUDE(getExtra_EXTRA_APPWIDGET_IDS)
Bundle extras = intent.getExtras();
int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
// END_INCLUDE(getExtra_EXTRA_APPWIDGET_IDS)
SharedPreferences prefs = context.getSharedPreferences(TestAppWidgetProvider.PREFS_NAME, 0);
String prefix = prefs.getString(PREF_PREFIX_KEY, "hai");
AppWidgetManager gm = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_appwidget);
views.setTextViewText(R.id.oh_hai_text, prefix + ": " + SystemClock.elapsedRealtime());
if (false) {
gm.updateAppWidget(appWidgetIds, views);
} else {
gm.updateAppWidget(new ComponentName("com.android.tests.appwidgethost", "com.android.tests.appwidgethost.TestAppWidgetProvider"), views);
}
}
}
use of android.content.SharedPreferences in project Android-CleanArchitecture by android10.
the class FileManager method writeToPreferences.
/**
* Write a value to a user preferences file.
*
* @param context {@link android.content.Context} to retrieve android user preferences.
* @param preferenceFileName A file name reprensenting where data will be written to.
* @param key A string for the key that will be used to retrieve the value in the future.
* @param value A long representing the value to be inserted.
*/
void writeToPreferences(Context context, String preferenceFileName, String key, long value) {
final SharedPreferences sharedPreferences = context.getSharedPreferences(preferenceFileName, Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong(key, value);
editor.apply();
}
use of android.content.SharedPreferences in project android-sms-relay by nyaruka.
the class RelayService method onNewSMS.
public void onNewSMS(String number, String message) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean process_messages = prefs.getBoolean("process_incoming", false);
TextMessageHelper helper = getHelper();
String keyword = prefs.getString("relay_password", "nyaruka").toLowerCase();
// if we aren't supposed to process messages, ignore this message
if (!process_messages) {
return;
}
// if someone sends 'keyword log' then send a log to the server
if (message.equalsIgnoreCase(keyword + " log")) {
RelayService.doSendLog = true;
int unsynced = helper.withStatus(getApplicationContext(), TextMessage.INCOMING, TextMessage.RECEIVED).size();
int erroredOut = helper.withStatus(getApplicationContext(), TextMessage.OUTGOING, TextMessage.ERRORED).size();
int erroredIn = helper.withStatus(getApplicationContext(), TextMessage.INCOMING, TextMessage.ERRORED).size();
modem.sendSms(number, "Log being sent. " + unsynced + " unsynced. " + erroredOut + " out errors. " + erroredIn + " in errors.", "-1");
return;
}
if (message.equalsIgnoreCase(keyword + " reset")) {
// change the label to true
doReset = true;
Log.d(TAG, "The reset process is set to " + Boolean.toString(doReset));
// start the check service for reset changes to take effect
kickService();
modem.sendSms(number, "Reset process has been started. ", "-1");
return;
}
if (message.equalsIgnoreCase(keyword + " clear")) {
Log.d(TAG, "Clearing all messages");
AndroidRelay.clearMessages(RelayService.this);
Toast.makeText(RelayService.this, "Messages cleared", Toast.LENGTH_LONG).show();
modem.sendSms(number, "All messages have been removed.", "-1");
return;
}
{
SharedPreferences.Editor editor = prefs.edit();
// process message with 'keyword data and change the network connection to specified mode
if (message.equalsIgnoreCase(keyword + " data")) {
Log.d(TAG, "Switching to data connection");
editor.putString("pref_net", "1");
editor.commit();
modem.sendSms(number, "Your preferred network is now set to 'MOBILE DATA'", "-1");
return;
}
// process message with 'keyword wifi and change the network connection to specified mode
if (message.equalsIgnoreCase(keyword + " wifi")) {
Log.d(TAG, "Switching to wifi connection");
editor.putString("pref_net", "0");
editor.commit();
modem.sendSms(number, "Your preferred network is now set to 'WIFI'", "-1");
return;
}
}
TextMessage msg = null;
msg = new TextMessage();
msg.number = number;
msg.text = message;
msg.created = new Date();
msg.direction = TextMessage.INCOMING;
msg.status = TextMessage.RECEIVED;
helper.createMessage(msg);
Log.d(TAG, "=== SMS IN:" + msg.number + ": " + msg.text);
MainActivity.updateMessage(msg);
kickService();
}
use of android.content.SharedPreferences in project android-sms-relay by nyaruka.
the class RelayService method markMessageDelivered.
public void markMessageDelivered(TextMessage msg) throws IOException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String deliveryURL = prefs.getString("delivery_url", null);
TextMessageHelper helper = getHelper();
Log.d(TAG, "Delivery URL: " + deliveryURL);
// no delivery url means we don't do anything
if (deliveryURL == null || deliveryURL.length() == 0) {
return;
}
if (msg.serverId <= 0) {
msg.status = TextMessage.DONE;
msg.error = "Ignored due to 0 id";
} else {
String url = deliveryURL + "&message_id=" + msg.serverId;
Log.d(TAG, "Sending: " + url);
try {
fetchURL(url);
msg.status = TextMessage.DONE;
msg.error = null;
Log.d(TAG, "Msg " + msg.serverId + " marked as delivered.");
} catch (IOException e) {
msg.status = TextMessage.SENT;
msg.error = e.getClass().getSimpleName() + ": " + e.getMessage();
throw e;
} catch (Throwable t) {
Log.d(TAG, "Got Error: " + t.getMessage(), t);
msg.status = TextMessage.SENT;
msg.error = t.getClass().getSimpleName() + ": " + t.getMessage();
} finally {
helper.updateMessage(msg);
MainActivity.updateMessage(msg);
}
}
}
use of android.content.SharedPreferences in project android-sms-relay by nyaruka.
the class SettingsActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View title = getWindow().findViewById(android.R.id.title);
title.setPadding(5, 0, 0, 0);
addPreferencesFromResource(R.xml.preferences);
// Add a listener for relay password
Preference relayPassword = (Preference) findPreference("relay_password");
relayPassword.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
String password = newValue.toString();
Log.d(TAG, "New password " + password);
return true;
}
});
Preference prefNetwork = (Preference) findPreference("pref_net");
prefNetwork.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d(TAG, "Your preferred network has been changed to: " + newValue);
return true;
}
});
// Add a listener for our clear button, with a confirmation
Preference clearMessages = (Preference) findPreference("clear_messages");
clearMessages.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
// show a confirmation dialog
new AlertDialog.Builder(SettingsActivity.this).setTitle("WARNING").setMessage("This will remove all messages, including ones not yet sent to the server or mobile phones.\n\nThis operation cannot be undone.").setPositiveButton("Reset", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
AndroidRelay.clearMessages(SettingsActivity.this);
Toast.makeText(SettingsActivity.this, "Messages cleared", Toast.LENGTH_LONG).show();
}
}).setNegativeButton(android.R.string.cancel, null).show();
return true;
}
});
Preference sendLog = (Preference) findPreference("send_log");
sendLog.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
RelayService.doSendLog = true;
WakefulIntentService.scheduleAlarms(new com.nyaruka.androidrelay.AlarmListener(1), getApplicationContext());
Toast.makeText(SettingsActivity.this, "Log will be sent shortly.", Toast.LENGTH_LONG).show();
return true;
}
});
Preference updateInterval = (Preference) findPreference("update_interval");
updateInterval.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
WakefulIntentService.scheduleAlarms(new com.nyaruka.androidrelay.AlarmListener(Long.parseLong(newValue.toString())), SettingsActivity.this);
Log.d(TAG, "Rescheduling alarms based on new update_interval value: " + newValue);
return true;
}
});
Preference setRapidHost = (Preference) findPreference("router_hostname");
setRapidHost.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
String hostname = newValue.toString();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
String backend = prefs.getString("router_backend", "android");
String password = prefs.getString("router_password", null);
updateEndpoints(hostname, backend, password);
return true;
}
});
Preference setRapidBackend = (Preference) findPreference("router_backend");
setRapidBackend.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
String backend = newValue.toString();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
String hostname = prefs.getString("router_hostname", null);
String password = prefs.getString("router_password", null);
updateEndpoints(hostname, backend, password);
return true;
}
});
Preference setRapidPassword = (Preference) findPreference("router_password");
setRapidPassword.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
String password = newValue.toString();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
String backend = prefs.getString("router_backend", "android");
String hostname = prefs.getString("router_hostname", null);
updateEndpoints(hostname, backend, password);
return true;
}
});
}
Aggregations