Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@
</intent-filter>
</receiver>

<receiver android:name=".providers.TextSecureAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/textsecure_appwidget_info" />
</receiver>

<receiver android:name=".service.SmsListener"
android:permission="android.permission.BROADCAST_SMS"
android:enabled="true"
Expand Down
16 changes: 16 additions & 0 deletions res/drawable/circle_badge.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dip"/>
<solid
android:color="#F00" />
<stroke
android:width="1dip"
android:color="#FFF" />
<padding
android:left="3dip"
android:right="3dip"
android:top="3dip"
android:bottom="3dip" />
</shape>
34 changes: 34 additions & 0 deletions res/layout/textsecure_appwidget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/icon_view"
android:background="@drawable/icon"
android:layout_gravity="left"/>

<TextView
android:layout_alignParentRight="true"
android:layout_width="25dip"
android:layout_height="25dip"
android:text="0"
android:textColor="#FFFFFF"
android:id="@+id/unread_count_text"
android:background="@drawable/circle_badge"
android:gravity="center_vertical|center_horizontal"
android:layout_alignParentEnd="false"
android:layout_gravity="right" />
</FrameLayout>

</LinearLayout>
11 changes: 11 additions & 0 deletions res/xml/textsecure_appwidget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:updatePeriodMillis="1800000"
android:previewImage="@drawable/icon"
android:initialLayout="@layout/textsecure_appwidget"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen|keyguard">
</appwidget-provider>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
Expand All @@ -41,6 +43,7 @@
import org.thoughtcrime.securesms.RoutingActivity;
import org.thoughtcrime.securesms.contacts.ContactPhotoFactory;
import org.thoughtcrime.securesms.database.PushDatabase;
import org.thoughtcrime.securesms.providers.TextSecureAppWidgetProvider;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.whispersystems.textsecure.crypto.MasterSecret;
Expand Down Expand Up @@ -98,7 +101,6 @@ public static void notifyMessageDeliveryFailed(Context context, Recipients recip
}
}


public static void updateNotification(Context context, MasterSecret masterSecret) {
if (!TextSecurePreferences.isNotificationsEnabled(context)) {
return;
Expand All @@ -120,6 +122,7 @@ public static void updateNotification(Context context, MasterSecret masterSecret
}
}


private static void updateNotification(Context context, MasterSecret masterSecret, boolean signal) {
Cursor telcoCursor = null;
Cursor pushCursor = null;
Expand All @@ -133,13 +136,16 @@ private static void updateNotification(Context context, MasterSecret masterSecre
{
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
.cancel(NOTIFICATION_ID);
TextSecureAppWidgetProvider.triggerUpdate(context, 0);
return;
}

NotificationState notificationState = constructNotificationState(context, masterSecret, telcoCursor);

appendPushNotificationState(context, masterSecret, notificationState, pushCursor);

TextSecureAppWidgetProvider.triggerUpdate(context, notificationState.getMessageCount());

if (notificationState.hasMultipleThreads()) {
sendMultipleThreadNotification(context, masterSecret, notificationState, signal);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* Copyright (C) 2014 Open Whisper Systems
*
* 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.thoughtcrime.securesms.providers;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.RoutingActivity;
import org.thoughtcrime.securesms.database.DatabaseFactory;

import java.util.List;

/**
* The provider for the TextSecure AppWidget
*
* @author Lukas Barth
*/
public class TextSecureAppWidgetProvider extends AppWidgetProvider {

final static String UNREAD_COUNT = "unreadCount";

public static void triggerUpdate(Context context, int unread) {
AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
ComponentName widgetComponent = new ComponentName(context, TextSecureAppWidgetProvider.class);
int[] widgetIds = widgetManager.getAppWidgetIds(widgetComponent);
// widget notification
Intent updateIntent = new Intent();
updateIntent.putExtra(UNREAD_COUNT, unread);
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
updateIntent.setClass(context, TextSecureAppWidgetProvider.class);
context.sendBroadcast(updateIntent);
// Samsung TouchWiz notification
String launcherClassName = getLauncherClassName(context);
if (launcherClassName != null) {
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", unread);
intent.putExtra("badge_count_package_name", context.getPackageName());
intent.putExtra("badge_count_class_name", launcherClassName);
context.sendBroadcast(intent);
}
// Sony BadgeReceiver notification
Intent intent = new Intent();
intent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", RoutingActivity.class.getCanonicalName());
intent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", "org.thoughtcrime.securesms");
intent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", Integer.toString(unread));
if(unread>0)
intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", true);
else
intent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", false);
context.sendBroadcast(intent);
}

private static String getLauncherClassName(Context context) {
PackageManager pm = context.getPackageManager();

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolveInfos) {
String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
if (pkgName.equalsIgnoreCase(context.getPackageName())) {
String className = resolveInfo.activityInfo.name;
return className;
}
}
return null;
}

public void onReceive(Context context, Intent intent) {
// Protect against rogue update broadcasts (not really a security issue,
// just filter bad broacasts out so subclasses are less likely to crash).
String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
Bundle extras = intent.getExtras();
if (extras != null) {
int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
int unread = extras.getInt(UNREAD_COUNT);
if (appWidgetIds != null && appWidgetIds.length > 0) {
final int n = appWidgetIds.length;

for (int i = 0; i < n; i++) {
int appWidgetId = appWidgetIds[i];

Intent clickIntent = new Intent(context, RoutingActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clickIntent, 0);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.textsecure_appwidget);
views.setOnClickPendingIntent(R.id.icon_view, pendingIntent);

if (unread > 0) {
if (unread > 9)
views.setTextViewText(R.id.unread_count_text, "9+");
else
views.setTextViewText(R.id.unread_count_text, Integer.toString(unread));
views.setViewVisibility(R.id.unread_count_text, View.VISIBLE);
} else {
views.setViewVisibility(R.id.unread_count_text, View.INVISIBLE);
}

AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, views);
}
}
}
} else
super.onReceive(context, intent);
}
}