Browse Source

Added widget

main
M66B 6 years ago
parent
commit
b3c7bd1429
6 changed files with 109 additions and 0 deletions
  1. +15
    -0
      app/src/main/AndroidManifest.xml
  2. +4
    -0
      app/src/main/java/eu/faircode/email/ServiceSynchronize.java
  3. +53
    -0
      app/src/main/java/eu/faircode/email/Widget.java
  4. +26
    -0
      app/src/main/res/layout/widget.xml
  5. +3
    -0
      app/src/main/res/values/colors.xml
  6. +8
    -0
      app/src/main/res/xml/widget.xml

+ 15
- 0
app/src/main/AndroidManifest.xml View File

@ -11,6 +11,10 @@
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-feature
android:name="android.software.app_widgets"
android:required="false" />
<application
android:name=".ApplicationEx"
android:allowBackup="false"
@ -103,6 +107,17 @@
android:resource="@xml/fileprovider_paths" />
</provider>
<receiver
android:name=".Widget"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget" />
</receiver>
<receiver android:name=".ReceiverAutostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />


+ 4
- 0
app/src/main/java/eu/faircode/email/ServiceSynchronize.java View File

@ -215,6 +215,8 @@ public class ServiceSynchronize extends LifecycleService {
serviceManager.onLost(null);
Widget.update(this, -1);
stopForeground(true);
NotificationManager nm = getSystemService(NotificationManager.class);
@ -339,6 +341,8 @@ public class ServiceSynchronize extends LifecycleService {
// https://developer.android.com/training/notify-user/group
List<Notification> notifications = new ArrayList<>();
Widget.update(this, messages.size());
if (messages.size() == 0)
return notifications;


+ 53
- 0
app/src/main/java/eu/faircode/email/Widget.java View File

@ -0,0 +1,53 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail 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.
NetGuard 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 NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
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.widget.RemoteViews;
public class Widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
update(appWidgetIds, appWidgetManager, context, -1);
}
static void update(Context context, int count) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetIds[] = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, Widget.class));
update(appWidgetIds, appWidgetManager, context, count);
}
private static void update(int[] appWidgetIds, AppWidgetManager appWidgetManager, Context context, int count) {
Intent intent = new Intent(context, ActivityView.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(context, ActivityView.REQUEST_UNIFIED, intent, PendingIntent.FLAG_UPDATE_CURRENT);
for (int id : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widget, pi);
views.setTextViewText(R.id.tvCount, count < 0 ? "?" : Integer.toString(count));
appWidgetManager.updateAppWidget(id, views);
}
}
}

+ 26
- 0
app/src/main/res/layout/widget.xml View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorWidgetBackground"
android:orientation="vertical"
android:padding="6dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/baseline_mail_24"
android:tint="@color/colorWidgetForeground" />
<TextView
android:id="@+id/tvCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="?"
android:textColor="@color/colorWidgetForeground"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>

+ 3
- 0
app/src/main/res/values/colors.xml View File

@ -7,6 +7,9 @@
<color name="colorActionForeground">#fff</color>
<color name="colorActionForegroundDisabled">#ccc</color>
<color name="colorWidgetForeground">#fff</color>
<color name="colorWidgetBackground">#3f000000</color>
<color name="lightColorUnread">#000</color>
<color name="lightColorItemSelected">#ccc</color>
<color name="lightColorSeparator">#aaa</color>


+ 8
- 0
app/src/main/res/xml/widget.xml View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minWidth="40dp"
android:minHeight="40dp"
android:previewImage="@drawable/baseline_mail_24"
android:resizeMode="none"
android:updatePeriodMillis="0" />

Loading…
Cancel
Save