Skip to content

Commit

Permalink
extension works, added pref for privacy mode
Browse files Browse the repository at this point in the history
  • Loading branch information
burntcookie90 committed Dec 23, 2013
1 parent d7598de commit c07324b
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 76 deletions.
218 changes: 160 additions & 58 deletions .idea/workspace.xml

Large diffs are not rendered by default.

48 changes: 30 additions & 18 deletions lastsms/src/main/AndroidManifest.xml
@@ -1,34 +1,46 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dwak.lastsms"
android:versionCode="1"
android:versionName="1.0">
package="com.dwak.lastsms"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="19" />
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="19"/>

<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />

<service
android:name=".LastSMSExtension"
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:permission="com.google.android.apps.dashclock.permission.READ_EXTENSION_DATA" >
android:theme="@style/AppTheme">

<service
android:name=".LastSMSExtension"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:permission="com.google.android.apps.dashclock.permission.READ_EXTENSION_DATA">
<intent-filter>
<action android:name="com.google.android.apps.dashclock.Extension" />
<action android:name="com.google.android.apps.dashclock.Extension"/>
</intent-filter>

<meta-data
android:name="protocolVersion"
android:value="1" />
android:name="protocolVersion"
android:value="2"/>
<meta-data
android:name="description"
android:value="@string/app_desc" >
android:name="description"
android:value="@string/app_desc">
</meta-data>
<meta-data
android:name="settingsActivity"
android:value=".LastSMSPreferences" >
</meta-data>
</service>


<activity
android:name=".LastSMSPreferences"
android:exported="true"
android:label="@string/title_settings" />
</application>

</manifest>
54 changes: 54 additions & 0 deletions lastsms/src/main/java/com/dwak/lastsms/LastSMSExtension.java
@@ -1,14 +1,68 @@
package com.dwak.lastsms;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.util.Log;
import com.google.android.apps.dashclock.api.DashClockExtension;
import com.google.android.apps.dashclock.api.ExtensionData;

public class LastSMSExtension extends DashClockExtension {

private static final String TAG = LastSMSExtension.class.getSimpleName();
private SMSData mSMSData;
public static final String PREF_PRIVACY = "pref_privacy";
private SharedPreferences mSharedPreferences;
private boolean isInPrivacyMode;

@Override
protected void onUpdateData(int arg0) {
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
isInPrivacyMode = mSharedPreferences.getBoolean("pref_privacy", false);
Uri uri = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uri, null, null, null, null);
Cursor cur2;
Log.d(TAG, "LastSMS onUpdateData");

if (c != null) {
if(c.getCount() != 0){
c.moveToFirst();
mSMSData = new SMSData();
mSMSData.setBody(c.getString(c.getColumnIndexOrThrow("body")));
mSMSData.setNumber(c.getString(c.getColumnIndexOrThrow("address")));
Log.d(TAG, mSMSData.getNumber() + " " + mSMSData.getBody());
Uri uri2 = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("tel:" + mSMSData.getNumber()));
cur2 = getContentResolver().query(uri2, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);

if (cur2 != null) {
if (cur2.moveToFirst()) {
mSMSData.setSenderName(cur2.getString(0));
} else {
mSMSData.setSenderName("Unknown");
}
} else {
mSMSData.setSenderName("Unknown");
}

if(cur2 != null){
cur2.close();
}
}
c.close();
}

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
String expandedBody = isInPrivacyMode ? "" : mSMSData.getBody();
publishUpdate(new ExtensionData()
.visible(true)
.icon(R.drawable.ic_launcher)
.status(mSMSData.getSenderName())
.expandedBody(expandedBody)
.clickIntent(sendIntent));
}

}
85 changes: 85 additions & 0 deletions lastsms/src/main/java/com/dwak/lastsms/LastSMSPreferences.java
@@ -0,0 +1,85 @@
package com.dwak.lastsms;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MenuItem;

public class LastSMSPreferences extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setIcon(R.drawable.ic_launcher);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setupSimplePreferencesScreen();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("deprecation")
private void setupSimplePreferencesScreen() {
// In the simplified UI, fragments are not used at all and we instead
// use the older PreferenceActivity APIs.

// Add 'general' preferences.
addPreferencesFromResource(R.xml.prefs);

// Bind the summaries of EditText/List/Dialog/Ringtone preferences to
// their values. When their values change, their summaries are updated
// to reflect the new value, per the Android Design guidelines.
bindPreferenceSummaryToValue(findPreference(LastSMSExtension.PREF_PRIVACY));
}

/**
* Binds a preference's summary to its value. More specifically, when the preference's value is
* changed, its summary (line of text below the preference title) is updated to reflect the
* value. The summary is also immediately updated upon calling this method. The exact display
* format is dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getBoolean(preference.getKey(), false));
}

/**
* A preference value change listener that updates the preference's summary to reflect its new
* value.
*/
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
if (preference instanceof CheckBoxPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
Log.v("Pref", "Check toggled " + value.toString());
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(preference.getContext()).edit();
editor.putBoolean("pref_privacy", ((Boolean)value).booleanValue());
// Set the summary to reflect the new value.
}
return true;
}
};
}
34 changes: 34 additions & 0 deletions lastsms/src/main/java/com/dwak/lastsms/SMSData.java
@@ -0,0 +1,34 @@
package com.dwak.lastsms;

/**
* Created by vishnu on 12/22/13.
*/
public class SMSData {
private String mNumber;
private String mBody;
private String mSenderName;

public String getSenderName() {
return mSenderName;
}

public void setSenderName(String senderName) {
mSenderName = senderName;
}

public String getNumber() {
return mNumber;
}

public void setNumber(String number) {
mNumber = number;
}

public String getBody() {
return mBody;
}

public void setBody(String body) {
mBody = body;
}
}
4 changes: 4 additions & 0 deletions lastsms/src/main/res/values/strings.xml
@@ -1,4 +1,8 @@
<resources>
<string name="app_name">Last SMS</string>
<string name="app_desc">Displays the Last SMS in your inbox</string>
<string name="pref_privacy_desc">On to show message body</string>
<string name="pref_privacy">pref_privacy</string>
<string name="pref_privacy_title">Privacy mode</string>
<string name="title_settings">Last SMS Preferences</string>
</resources>
9 changes: 9 additions & 0 deletions lastsms/src/main/res/xml/prefs.xml
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:title="@string/pref_privacy_title"
android:enabled="true"
android:defaultValue="false"
android:key="@string/pref_privacy"
android:summary="@string/pref_privacy_desc"/>
</PreferenceScreen>

0 comments on commit c07324b

Please sign in to comment.