package com.noamwolf.android.androidfound.persist;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import com.noamwolf.android.androidfound.AndroidFound;
import com.noamwolf.android.androidfound.SettingsActivity;
import com.noamwolf.android.androidfound.Owner.Columns;
public class BootCompletedReceiver extends BroadcastReceiver {
private static final String AF_TAG = "AF_persist_boot_completed";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(AF_TAG, "got persist broadcast");
Cursor cursor = getCursor(context, intent);
if (cursor != null && cursor.moveToFirst()) {
Log.i(AF_TAG, "got persist data");
String name = clearIfNull(cursor.getString(0));
String company = clearIfNull(cursor.getString(1));
final String email = clearIfNull(cursor.getString(2));
final String phone = clearIfNull(cursor.getString(3));
// String imageUri = cursor.getString(4);
final String foundMessage = clearIfNull(cursor.getString(5));
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean showNumber = sharedPreferences.getBoolean(
SettingsActivity.KEY_FORMAT_NUMBER, true);
boolean showEmail = sharedPreferences.getBoolean(
SettingsActivity.KEY_FORMAT_EMAIL, true);
boolean showCompany = sharedPreferences.getBoolean(
SettingsActivity.KEY_FORMAT_COMPANY, true);
boolean showMessage = sharedPreferences.getBoolean(
SettingsActivity.KEY_FORMAT_MSG, true);
String msg = name;
if (showCompany && company.trim().length() > 0) {
msg += "\n" + company;
}
if (showEmail && email.trim().length() > 0) {
msg += "\n" + email;
}
if (showNumber && phone.trim().length() > 0) {
msg += "\n" + phone;
}
if (showMessage && foundMessage.trim().length() > 0) {
msg += "\n" + foundMessage;
}
if (msg.length() > 0) {
Settings.System.putString(context.getContentResolver(),
Settings.System.NEXT_ALARM_FORMATTED, msg);
Log.i(AF_TAG, "set persist data");
}
}
}
private Cursor getCursor(Context context, Intent intent) {
if (intent.getData() == null) {
intent.setData(Columns.CONTENT_URI);
}
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(intent.getData(), AndroidFound.PROJECTION, null,
null, "name ASC");
return cursor;
}
private String clearIfNull(String s) {
if (s == null) {
s = "";
}
return s;
}
}
|