mirror of
https://github.com/gsantner/dandelion
synced 2025-12-15 08:41:10 +01:00
Merge branch master into customTabs
This commit is contained in:
commit
3af1d120d7
44 changed files with 289 additions and 74 deletions
|
|
@ -26,6 +26,8 @@ import android.app.AlarmManager;
|
|||
import android.app.AlertDialog;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.graphics.drawable.LayerDrawable;
|
||||
import android.support.v4.view.MenuItemCompat;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
|
|
@ -81,6 +83,7 @@ import com.github.dfa.diaspora_android.data.PodUserProfile;
|
|||
import com.github.dfa.diaspora_android.listener.WebUserProfileChangedListener;
|
||||
import com.github.dfa.diaspora_android.receivers.OpenExternalLinkReceiver;
|
||||
import com.github.dfa.diaspora_android.receivers.UpdateTitleReceiver;
|
||||
import com.github.dfa.diaspora_android.ui.BadgeDrawable;
|
||||
import com.github.dfa.diaspora_android.ui.ContextMenuWebView;
|
||||
import com.github.dfa.diaspora_android.ui.CustomWebViewClient;
|
||||
import com.github.dfa.diaspora_android.util.CustomTabHelpers.BrowserFallback;
|
||||
|
|
@ -754,21 +757,16 @@ public class MainActivity extends AppCompatActivity
|
|||
|
||||
@Override
|
||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||
Log.i(App.TAG, "MainActivity.onPrepareOptionsMenu()");
|
||||
MenuItem itemNotification = menu.findItem(R.id.action_notifications);
|
||||
if (itemNotification != null) {
|
||||
if (podUserProfile.getNotificationCount() > 0) {
|
||||
itemNotification.setIcon(R.drawable.ic_notifications_colored_48px);
|
||||
} else {
|
||||
itemNotification.setIcon(R.drawable.ic_notifications_white_48px);
|
||||
}
|
||||
MenuItem item;
|
||||
|
||||
MenuItem itemConversation = menu.findItem(R.id.action_conversations);
|
||||
if (podUserProfile.getUnreadMessagesCount() > 0) {
|
||||
itemConversation.setIcon(R.drawable.ic_email_colored_48px);
|
||||
} else {
|
||||
itemConversation.setIcon(R.drawable.ic_mail_white_48px);
|
||||
}
|
||||
if ((item = menu.findItem(R.id.action_notifications)) != null) {
|
||||
LayerDrawable icon = (LayerDrawable) item.getIcon();
|
||||
BadgeDrawable.setBadgeCount(this, icon, podUserProfile.getNotificationCount());
|
||||
}
|
||||
|
||||
if ((item = menu.findItem(R.id.action_conversations)) != null) {
|
||||
LayerDrawable icon = (LayerDrawable) item.getIcon();
|
||||
BadgeDrawable.setBadgeCount(this, icon, podUserProfile.getUnreadMessagesCount());
|
||||
}
|
||||
return super.onPrepareOptionsMenu(menu);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,127 @@
|
|||
package com.github.dfa.diaspora_android.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ColorFilter;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.Typeface;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.LayerDrawable;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
|
||||
import com.github.dfa.diaspora_android.R;
|
||||
|
||||
public class BadgeDrawable extends Drawable {
|
||||
// Source: http://mobikul.com/adding-badge-count-on-menu-items-like-cart-notification-etc/
|
||||
private static final String BADGE_VALUE_OVERFLOW = "*";
|
||||
|
||||
private Paint badgeBackground;
|
||||
private Paint badgeStroke;
|
||||
private Paint badgeText;
|
||||
private Rect textRect = new Rect();
|
||||
|
||||
private String badgeValue = "";
|
||||
private boolean shouldDraw;
|
||||
|
||||
public BadgeDrawable(Context context) {
|
||||
float textSize = context.getResources().getDimension(R.dimen.textsize_badge_count);
|
||||
|
||||
badgeBackground = new Paint();
|
||||
badgeBackground.setColor(ContextCompat.getColor(context.getApplicationContext(), R.color.accent));
|
||||
badgeBackground.setAntiAlias(true);
|
||||
badgeBackground.setStyle(Paint.Style.FILL);
|
||||
badgeStroke = new Paint();
|
||||
badgeStroke.setColor(ContextCompat.getColor(context.getApplicationContext(), R.color.colorPrimaryDark));
|
||||
badgeStroke.setAntiAlias(true);
|
||||
badgeStroke.setStyle(Paint.Style.FILL);
|
||||
|
||||
badgeText = new Paint();
|
||||
badgeText.setColor(Color.WHITE);
|
||||
badgeText.setTypeface(Typeface.DEFAULT);
|
||||
badgeText.setTextSize(textSize);
|
||||
badgeText.setAntiAlias(true);
|
||||
badgeText.setTextAlign(Paint.Align.CENTER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas) {
|
||||
if (!shouldDraw) {
|
||||
return;
|
||||
}
|
||||
Rect bounds = getBounds();
|
||||
float width = bounds.right - bounds.left;
|
||||
float height = bounds.bottom - bounds.top;
|
||||
|
||||
// Position the badge in the top-right quadrant of the icon.
|
||||
float radius = ((Math.max(width, height) / 2)) / 2;
|
||||
float centerX = (width - radius - 1) + 5;
|
||||
float centerY = radius - 5;
|
||||
if (badgeValue.length() <= 2) {
|
||||
// Draw badge circle.
|
||||
canvas.drawCircle(centerX, centerY, (int) (radius + 7.5), badgeStroke);
|
||||
canvas.drawCircle(centerX, centerY, (int) (radius + 5.5), badgeBackground);
|
||||
} else {
|
||||
canvas.drawCircle(centerX, centerY, (int) (radius + 8.5), badgeStroke);
|
||||
canvas.drawCircle(centerX, centerY, (int) (radius + 6.5), badgeBackground);
|
||||
//canvas.drawRoundRect(radius, radius, radius, radius, 10, 10, badgeBackground);
|
||||
}
|
||||
// Draw badge count text inside the circle.
|
||||
badgeText.getTextBounds(badgeValue, 0, badgeValue.length(), textRect);
|
||||
float textHeight = textRect.bottom - textRect.top;
|
||||
float textY = centerY + (textHeight / 2f);
|
||||
if (badgeValue.length() > 2)
|
||||
canvas.drawText(BADGE_VALUE_OVERFLOW, centerX, textY, badgeText);
|
||||
else
|
||||
canvas.drawText(badgeValue, centerX, textY, badgeText);
|
||||
}
|
||||
|
||||
/*
|
||||
Sets the count (i.e notifications) to display.
|
||||
*/
|
||||
public void setCount(String count) {
|
||||
badgeValue = count;
|
||||
|
||||
// Only draw a badge if there are notifications.
|
||||
shouldDraw = !count.equalsIgnoreCase("0");
|
||||
invalidateSelf();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAlpha(int alpha) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColorFilter(ColorFilter cf) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOpacity() {
|
||||
return PixelFormat.UNKNOWN;
|
||||
}
|
||||
|
||||
public static void setBadgeCount(Context context, LayerDrawable icon, Integer count) {
|
||||
setBadgeCount(context, icon, count.toString());
|
||||
}
|
||||
|
||||
public static void setBadgeCount(Context context, LayerDrawable icon, String count) {
|
||||
|
||||
BadgeDrawable badge;
|
||||
|
||||
// Reuse drawable if possible
|
||||
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
|
||||
if (reuse != null && reuse instanceof BadgeDrawable) {
|
||||
badge = (BadgeDrawable) reuse;
|
||||
} else {
|
||||
badge = new BadgeDrawable(context);
|
||||
}
|
||||
|
||||
badge.setCount(count);
|
||||
icon.mutate();
|
||||
icon.setDrawableByLayerId(R.id.ic_badge, badge);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue