aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/utils/UnreadCountBadgeHelper.java
blob: 3e194c05e25191a351b06e52cfb53ddeb4f2801b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package eu.siacs.conversations.utils;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import eu.siacs.conversations.Config;

public class UnreadCountBadgeHelper {

	private static int count = 0;

	public static void update(final Context context,
							  final int count) {
		if (UnreadCountBadgeHelper.count != count) {
			UnreadCountBadgeHelper.count = count;
			Log.d(Config.LOGTAG, "updating read count to: " + count);
			final String packageName = context.getPackageName();
			final String activity = context.getPackageManager().getLaunchIntentForPackage(packageName).getComponent().getClassName();
			updateTeslaUnreadApi(context, packageName, activity, count);
			updateApexNotificationApi(context, packageName, activity, count);
		}
	}

	/*
	TeslaUnread API primarily used by Nova Launcher
	documented here: http://novalauncher.com/teslaunread-api/
	 */
	private static void updateTeslaUnreadApi(final Context context,
											 final String packageName,
											 final String activity,
											 final int count) {
		try {
			ContentValues contentValues = new ContentValues();
			contentValues.put("tag",packageName+"/"+activity);
			contentValues.put("count",count);
			context.getContentResolver().insert(
					Uri.parse("content://com.teslacoilsw.notifier/unread_count"),
					contentValues
					);
		} catch (IllegalArgumentException e) {
			return;
		} catch (Exception e) {
			return;
		}
	}

	/*
	Apex Launcher Notifications API used by Apex Launcher Pro and others (Smart Launcher 2)
	 */
	private static void updateApexNotificationApi(final Context context,
												  final String packageName,
												  final String activity,
												  final int count) {
		Intent intent = new Intent("com.anddoes.launcher.COUNTER_CHANGED");
		intent.putExtra("package",packageName);
		//intent.putExtra("class",packageName+"."+activity);
		intent.putExtra("class",activity);
		intent.putExtra("count",count);
		context.sendBroadcast(intent);
	}
}