Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager string resource loading #623

Merged
merged 1 commit into from
Feb 4, 2018
Merged
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
3 changes: 2 additions & 1 deletion acra-core/src/main/java/org/acra/builder/ReportExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.acra.interaction.ReportInteractionExecutor;
import org.acra.sender.SenderServiceStarter;
import org.acra.util.ProcessFinisher;
import org.acra.util.ToastSender;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -199,7 +200,7 @@ public final void execute(@NonNull final ReportBuilder reportBuilder) {
@Override
public void run() {
Looper.prepare();
Toast.makeText(context, warning, Toast.LENGTH_LONG).show();
ToastSender.sendToast(context, warning, Toast.LENGTH_LONG);
Looper.loop();
}
}).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package org.acra.config;

import android.content.Context;
import android.support.annotation.NonNull;

import org.acra.ACRA;
import org.acra.ReportField;
import org.acra.annotation.BuilderMethod;
import org.acra.annotation.ConfigurationValue;
import org.acra.annotation.PreBuild;
import org.acra.annotation.Transform;

Expand Down Expand Up @@ -49,7 +52,7 @@ public final class BaseCoreConfigurationBuilder {
private final List<ConfigurationBuilder> configurationBuilders;
private List<Configuration> configurations;

BaseCoreConfigurationBuilder(@NonNull Class<?> app) {
BaseCoreConfigurationBuilder(@NonNull Context app) {
reportContentChanges = new EnumMap<>(ReportField.class);
configurationBuilders = new ArrayList<>();
//noinspection ForLoopReplaceableByForEach
Expand Down Expand Up @@ -100,15 +103,18 @@ Set<ReportField> transformReportContent(ReportField[] reportFields) {
* @param field the field to set
* @param enable if this field should be reported
*/
@BuilderMethod
public void setReportField(@NonNull ReportField field, boolean enable) {
this.reportContentChanges.put(field, enable);
}

@ConfigurationValue
@NonNull
List<Configuration> pluginConfigurations() {
return configurations;
}

@BuilderMethod
public <R extends ConfigurationBuilder> R getPluginConfigurationBuilder(Class<R> c) {
for (ConfigurationBuilder builder : configurationBuilders) {
if (c.isAssignableFrom(builder.getClass())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017
* Copyright (c) 2018 the ACRA team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.acra.config;

import android.content.Context;
import android.support.annotation.Keep;
import android.support.annotation.NonNull;

Expand All @@ -30,9 +31,9 @@ public interface ConfigurationBuilderFactory {
/**
* creates a new builder
*
* @param annotatedClass the class holding the annotation from which the builder should pull its values
* @param annotatedContext the context holding the annotation from which the builder should pull its values
* @return a new builder with values from the annotation
*/
@NonNull
ConfigurationBuilder create(@NonNull Class<?> annotatedClass);
ConfigurationBuilder create(@NonNull Context annotatedContext);
}
6 changes: 3 additions & 3 deletions acra-core/src/main/java/org/acra/sender/SenderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ protected void onHandleIntent(@Nullable final Intent intent) {
reportDistributor.distribute(report);
reportsSentCount++;
}
final int toastRes = reportsSentCount > 0 ? config.resReportSendSuccessToast() : config.resReportSendFailureToast();
if (toastRes != ACRAConstants.DEFAULT_RES_VALUE) {
ToastSender.sendToast(this, toastRes, Toast.LENGTH_LONG);
final String toast = reportsSentCount > 0 ? config.reportSendSuccessToast() : config.reportSendFailureToast();
if (toast != null) {
ToastSender.sendToast(this, toast, Toast.LENGTH_LONG);
}
} catch (Exception e) {
ACRA.log.e(LOG_TAG, "", e);
Expand Down
14 changes: 7 additions & 7 deletions acra-core/src/main/java/org/acra/util/ToastSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.content.Context;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.widget.Toast;

import org.acra.ACRA;
Expand All @@ -33,18 +32,19 @@
* @since 4.3.0
*/
public final class ToastSender {
private ToastSender(){}
private ToastSender() {
}

/**
* Sends a Toast and ensures that any Exception thrown during sending is handled.
*
* @param context Application context.
* @param toastResourceId Id of the resource to send as the Toast message.
* @param toastLength Length of the Toast.
* @param context Application context.
* @param toast toast message.
* @param toastLength Length of the Toast.
*/
public static void sendToast(@NonNull Context context, @StringRes int toastResourceId, @IntRange(from = 0, to = 1) int toastLength) {
public static void sendToast(@NonNull Context context, String toast, @IntRange(from = 0, to = 1) int toastLength) {
try {
Toast.makeText(context, toastResourceId, toastLength).show();
Toast.makeText(context, toast, toastLength).show();
} catch (RuntimeException e) {
ACRA.log.w(LOG_TAG, "Could not send crash Toast", e);
}
Expand Down
28 changes: 14 additions & 14 deletions acra-dialog/src/main/java/org/acra/dialog/CrashReportDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ protected void init(@Nullable Bundle savedInstanceState) {
*/
protected void buildAndShowDialog(@Nullable Bundle savedInstanceState) {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
final int titleResourceId = dialogConfiguration.resTitle();
if (titleResourceId != ACRAConstants.DEFAULT_RES_VALUE) {
dialogBuilder.setTitle(titleResourceId);
final String title = dialogConfiguration.title();
if (title != null) {
dialogBuilder.setTitle(title);
}
final int iconResourceId = dialogConfiguration.resIcon();
if (iconResourceId != ACRAConstants.DEFAULT_RES_VALUE) {
dialogBuilder.setIcon(iconResourceId);
}
dialogBuilder.setView(buildCustomView(savedInstanceState))
.setPositiveButton(getText(dialogConfiguration.resPositiveButtonText()), this)
.setNegativeButton(getText(dialogConfiguration.resNegativeButtonText()), this);
.setPositiveButton(dialogConfiguration.positiveButtonText(), this)
.setNegativeButton(dialogConfiguration.negativeButtonText(), this);

mDialog = dialogBuilder.create();
mDialog.setCanceledOnTouchOutside(false);
Expand Down Expand Up @@ -152,9 +152,9 @@ protected final void addViewToDialog(@NonNull View v) {
@NonNull
protected View getMainView() {
final TextView text = new TextView(this);
final int dialogTextId = dialogConfiguration.resText();
if (dialogTextId != ACRAConstants.DEFAULT_RES_VALUE) {
text.setText(getText(dialogTextId));
final String dialogText = dialogConfiguration.text();
if (dialogText != null) {
text.setText(dialogText);
}
return text;
}
Expand All @@ -166,10 +166,10 @@ protected View getMainView() {
*/
@Nullable
protected View getCommentLabel() {
final int commentPromptId = dialogConfiguration.resCommentPrompt();
if (commentPromptId != ACRAConstants.DEFAULT_RES_VALUE) {
final String commentPrompt = dialogConfiguration.commentPrompt();
if (commentPrompt != null) {
final TextView labelView = new TextView(this);
labelView.setText(getText(commentPromptId));
labelView.setText(commentPrompt);
return labelView;
}
return null;
Expand Down Expand Up @@ -198,10 +198,10 @@ protected EditText getCommentPrompt(@Nullable CharSequence savedComment) {
*/
@Nullable
protected View getEmailLabel() {
final int emailPromptId = dialogConfiguration.resEmailPrompt();
if (emailPromptId != ACRAConstants.DEFAULT_RES_VALUE) {
final String emailPrompt = dialogConfiguration.emailPrompt();
if (emailPrompt != null) {
final TextView labelView = new TextView(this);
labelView.setText(getText(emailPromptId));
labelView.setText(emailPrompt);
return labelView;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import android.support.annotation.NonNull;

import org.acra.annotation.BuilderMethod;
import org.acra.annotation.ConfigurationValue;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -39,11 +42,13 @@ public class BaseHttpConfigurationBuilder {
*
* @param headers A map associating HTTP header names to their values.
*/
@BuilderMethod
public void setHttpHeaders(@NonNull Map<String, String> headers) {
this.httpHeaders.clear();
this.httpHeaders.putAll(headers);
}

@ConfigurationValue
@NonNull
Map<String, String> httpHeaders() {
return httpHeaders;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.auto.service.AutoService;

import org.acra.ACRA;
import org.acra.ACRAConstants;
import org.acra.builder.ReportBuilder;
import org.acra.data.CrashReportData;
import org.acra.file.ReportLocator;
Expand Down Expand Up @@ -108,12 +107,12 @@ public boolean shouldSendReport(@NonNull Context context, @NonNull CoreConfigura
@Override
public void notifyReportDropped(@NonNull final Context context, @NonNull final CoreConfiguration config) {
final LimiterConfiguration limiterConfiguration = ConfigUtils.getPluginConfiguration(config, LimiterConfiguration.class);
if (limiterConfiguration.resIgnoredCrashToast() != ACRAConstants.DEFAULT_RES_VALUE) {
if (limiterConfiguration.ignoredCrashToast() != null) {
final Future<?> future = Executors.newSingleThreadExecutor().submit(new Runnable() {
@Override
public void run() {
Looper.prepare();
ToastSender.sendToast(context, limiterConfiguration.resIgnoredCrashToast(), Toast.LENGTH_LONG);
ToastSender.sendToast(context, limiterConfiguration.ignoredCrashToast(), Toast.LENGTH_LONG);
final Looper looper = Looper.myLooper();
if (looper != null) {
new Handler(looper).postDelayed(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@
* @return resource id of the custom email subject
* @since 5.0.1
*/
@StringRes int subject() default ACRAConstants.DEFAULT_RES_VALUE;
@StringRes int resSubject() default ACRAConstants.DEFAULT_RES_VALUE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ private void grantPermission(@NonNull Context context, Intent intent, String pac
*/
@NonNull
protected String buildSubject(@NonNull Context context) {
if (mailConfig.subject() != ACRAConstants.DEFAULT_RES_VALUE) {
return context.getString(mailConfig.subject());
final String subject = mailConfig.subject();
if (subject != null) {
return subject;
}
return context.getPackageName() + " Crash Report";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.google.auto.service.AutoService;

import org.acra.ACRA;
import org.acra.ACRAConstants;
import org.acra.config.ConfigUtils;
import org.acra.config.CoreConfiguration;
import org.acra.config.NotificationConfiguration;
Expand Down Expand Up @@ -76,39 +75,38 @@ public boolean performInteraction(@NonNull Context context, @NonNull CoreConfigu
final NotificationConfiguration notificationConfig = ConfigUtils.getPluginConfiguration(config, NotificationConfiguration.class);
//We have to create a channel on Oreo+, because notifications without one aren't allowed
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationChannel channel = new NotificationChannel(CHANNEL, context.getString(notificationConfig.resChannelName()), notificationConfig.resChannelImportance());
final NotificationChannel channel = new NotificationChannel(CHANNEL, notificationConfig.channelName(), notificationConfig.resChannelImportance());
channel.setSound(null, null);
if (notificationConfig.resChannelDescription() != ACRAConstants.DEFAULT_RES_VALUE) {
channel.setDescription(context.getString(notificationConfig.resChannelDescription()));
if (notificationConfig.channelDescription() != null) {
channel.setDescription(notificationConfig.channelDescription());
}
notificationManager.createNotificationChannel(channel);
}
//configure base notification
final NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL)
.setWhen(System.currentTimeMillis())
.setContentTitle(context.getString(notificationConfig.resTitle()))
.setContentText(context.getString(notificationConfig.resText()))
.setContentTitle(notificationConfig.title())
.setContentText(notificationConfig.text())
.setSmallIcon(notificationConfig.resIcon())
.setPriority(NotificationCompat.PRIORITY_HIGH);
//add ticker if set
if (notificationConfig.resTickerText() != ACRAConstants.DEFAULT_RES_VALUE) {
notification.setTicker(context.getString(notificationConfig.resTickerText()));
if (notificationConfig.tickerText() != null) {
notification.setTicker(notificationConfig.tickerText());
}
final PendingIntent sendIntent = getSendIntent(context, config, reportFile);
final PendingIntent discardIntent = getDiscardIntent(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && notificationConfig.resSendWithCommentButtonText() != ACRAConstants.DEFAULT_RES_VALUE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && notificationConfig.sendWithCommentButtonText() != null) {
final RemoteInput.Builder remoteInput = new RemoteInput.Builder(KEY_COMMENT);
if (notificationConfig.resCommentPrompt() != ACRAConstants.DEFAULT_RES_VALUE) {
remoteInput.setLabel(context.getString(notificationConfig.resCommentPrompt()));
if (notificationConfig.commentPrompt() != null) {
remoteInput.setLabel(notificationConfig.commentPrompt());
}
notification.addAction(new NotificationCompat.Action.Builder(notificationConfig.resSendWithCommentButtonIcon(),
context.getString(notificationConfig.resSendWithCommentButtonText()), sendIntent)
notification.addAction(new NotificationCompat.Action.Builder(notificationConfig.resSendWithCommentButtonIcon(), notificationConfig.sendWithCommentButtonText(), sendIntent)
.addRemoteInput(remoteInput.build()).build());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
final RemoteViews bigView = getBigView(context, notificationConfig);
notification.addAction(notificationConfig.resSendButtonIcon(), context.getString(notificationConfig.resSendButtonText()), sendIntent)
.addAction(notificationConfig.resDiscardButtonIcon(), context.getString(notificationConfig.resDiscardButtonText()), discardIntent)
notification.addAction(notificationConfig.resSendButtonIcon(), notificationConfig.sendButtonText(), sendIntent)
.addAction(notificationConfig.resDiscardButtonIcon(), notificationConfig.discardButtonText(), discardIntent)
.setCustomContentView(getSmallView(context, notificationConfig, sendIntent, discardIntent))
.setCustomBigContentView(bigView)
.setCustomHeadsUpContentView(bigView)
Expand Down Expand Up @@ -139,8 +137,8 @@ private PendingIntent getDiscardIntent(@NonNull Context context) {

private RemoteViews getSmallView(@NonNull Context context, @NonNull NotificationConfiguration notificationConfig, @NonNull PendingIntent sendIntent, @NonNull PendingIntent discardIntent) {
final RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.notification_small);
view.setTextViewText(R.id.text, context.getString(notificationConfig.resText()));
view.setTextViewText(R.id.title, context.getString(notificationConfig.resTitle()));
view.setTextViewText(R.id.text, notificationConfig.text());
view.setTextViewText(R.id.title, notificationConfig.title());
view.setImageViewResource(R.id.button_send, notificationConfig.resSendButtonIcon());
view.setImageViewResource(R.id.button_discard, notificationConfig.resDiscardButtonIcon());
view.setOnClickPendingIntent(R.id.button_send, sendIntent);
Expand All @@ -150,8 +148,8 @@ private RemoteViews getSmallView(@NonNull Context context, @NonNull Notification

private RemoteViews getBigView(@NonNull Context context, @NonNull NotificationConfiguration notificationConfig) {
final RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.notification_big);
view.setTextViewText(R.id.text, context.getString(notificationConfig.resText()));
view.setTextViewText(R.id.title, context.getString(notificationConfig.resTitle()));
view.setTextViewText(R.id.text, notificationConfig.text());
view.setTextViewText(R.id.title, notificationConfig.title());
return view;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ToastInteraction() {
@Override
public boolean performInteraction(@NonNull Context context, @NonNull CoreConfiguration config, @NonNull File reportFile) {
Looper.prepare();
ToastSender.sendToast(context, ConfigUtils.getPluginConfiguration(config, ToastConfiguration.class).resText(), Toast.LENGTH_LONG);
ToastSender.sendToast(context, ConfigUtils.getPluginConfiguration(config, ToastConfiguration.class).text(), Toast.LENGTH_LONG);
final Looper looper = Looper.myLooper();
if(looper != null) {
new Handler(looper).postDelayed(new Runnable() {
Expand Down
7 changes: 4 additions & 3 deletions annotationprocessor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.google.auto.service:auto-service:$autoServiceVersion"
compile 'com.squareup:javapoet:1.9.0'
compile 'org.apache.commons:commons-lang3:3.6'
compile 'org.apache.commons:commons-lang3:3.7'
compile 'org.apache.commons:commons-text:1.1'
compile project(':annotations')
compile project(':acra-javacore')
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"
sourceCompatibility = "1.9"
targetCompatibility = "1.9"
Loading