Skip to content

Commit

Permalink
Merge branch 'master' into prod
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiv4nov committed Nov 14, 2019
2 parents 32ce283 + 4f85f81 commit 80b0a3b
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 95 deletions.
4 changes: 2 additions & 2 deletions adguard_cb/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
minSdkVersion 21
targetSdkVersion 28
applicationId 'com.adguard.android.contentblocker'
versionCode 22000006
versionName '2.5.5'
versionCode 22000007
versionName '2.5.6'
signingConfig signingConfigs.config
}

Expand Down
11 changes: 11 additions & 0 deletions adguard_cb/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@
</intent-filter>
</receiver>

<receiver android:name=".receiver.StarsCountReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.adguard.android.contentblocker.count_action_1" />
<action android:name="com.adguard.android.contentblocker.count_action_2" />
<action android:name="com.adguard.android.contentblocker.count_action_3" />
<action android:name="com.adguard.android.contentblocker.count_action_4" />
<action android:name="com.adguard.android.contentblocker.count_action_5" />
</intent-filter>
</receiver>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
*/
public class BrowserUtils {

private static boolean dialogAboutChromeShowed = false;

private static final String YANDEX = "yandex";
private static final String SAMSUNG = "samsung";

Expand Down Expand Up @@ -205,15 +203,9 @@ public static boolean isYandexBrowserAvailable(Context context) {
@SuppressLint("InflateParams")
public static void showBrowserInstallDialog(final Context context) {
View dialogLayout = LayoutInflater.from(context).inflate(R.layout.select_browser_dialog, null);
dialogAboutChromeShowed = false;

final AlertDialog dialog = new AlertDialog.Builder(context, R.style.AlertDialog)
.setNegativeButton(android.R.string.cancel, (dialog1, which) -> {
dialog1.dismiss();
if (!dialogAboutChromeShowed) {
showProductsDialog(context, true);
}
})
.setNegativeButton(android.R.string.cancel, null)
.setView(dialogLayout).create();

View browserItem = dialogLayout.findViewById(R.id.browser_yandex);
Expand All @@ -229,35 +221,32 @@ public static void showBrowserInstallDialog(final Context context) {
});

dialogLayout.findViewById(R.id.others_product_card).setOnClickListener(v -> {
showProductsDialog(context, false);
showProductsDialog(context);
});

dialog.show();
centerDialogButton(dialog);
}

@SuppressLint("InflateParams")
private static void showProductsDialog(final Context context, boolean hideProductsButton) {
private static void showProductsDialog(final Context context) {
View dialogLayout = LayoutInflater.from(context).inflate(R.layout.products_dialog, null);

final AlertDialog dialog = new AlertDialog.Builder(context, R.style.AlertDialog)
.setNegativeButton(hideProductsButton ? R.string.close : R.string.back, null)
.setNegativeButton(R.string.back, null)
.setView(dialogLayout).create();

TextView textView = dialogLayout.findViewById(R.id.dialog_text);
textView.setText(Html.fromHtml(context.getString(R.string.chrome_dialog_text) + "<br/><br/>" + context.getString(R.string.select_browser_dialog_adguard_products_message)));
textView.setText(Html.fromHtml(context.getString(R.string.chrome_dialog_text)));
textView.setMovementMethod(LinkMovementMethod.getInstance());

View productsButton = dialogLayout.findViewById(R.id.go_to_products);
productsButton.setOnClickListener(v -> {
dialogLayout.findViewById(R.id.go_to_products).setOnClickListener(v -> {
String url = AppLink.Website.getOtherProductUrl(context, "chrome_dialog");
NavigationHelper.openWebSite(context, url);
});
productsButton.setVisibility(hideProductsButton ? View.GONE : View.VISIBLE);

dialog.show();
centerDialogButton(dialog);
dialogAboutChromeShowed = true;
}

public static void startYandexBrowser(Context context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.adguard.android.contentblocker.receiver;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.adguard.android.contentblocker.ServiceLocator;
import com.adguard.android.contentblocker.service.NotificationServiceImpl;
import com.adguard.android.contentblocker.ui.MainActivity;
import com.adguard.android.contentblocker.ui.utils.NavigationHelper;

import org.apache.commons.lang3.StringUtils;

import static android.content.Context.NOTIFICATION_SERVICE;

public class StarsCountReceiver extends BroadcastReceiver {

private static final long REDIRECTION_DELAY = 300;

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (StringUtils.isEmpty(action)) {
return;
}

int count = ServiceLocator.getInstance(context).getNotificationService().showRateAppNotification(action);
// If something wrong with action, the user can rate us anyway.
if (count == 0) {
NavigationHelper.redirectToPlayMarket(context);
} else {
redirectWithDelay(context, count);
}
}

/**
* Redirects to Google Play market or to feedback dialog.
* {@link Handler#postDelayed} used to fill selected stars count explicit before redirection
*
* @param count Selected stars count
*/
private void redirectWithDelay(Context context, final int count) {
new Handler(Looper.getMainLooper()).postDelayed(() -> {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (notificationManager != null) {
// Cancel current 'rate app' notification and close notification bar before redirection
notificationManager.cancel(NotificationServiceImpl.RATE_NOTIFICATION_ID);
Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(closeIntent);
}

if (count > 3) {
NavigationHelper.redirectToPlayMarket(context);
} else {
Bundle bundle = new Bundle();
bundle.putInt(MainActivity.STARS_COUNT, count);
NavigationHelper.redirectToActivity(context, MainActivity.class, bundle);
}
}, REDIRECTION_DELAY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.adguard.android.contentblocker.service;

import androidx.annotation.NonNull;

/**
* Application notifications service. Responsible for notifications and toast messages.
*/
Expand Down Expand Up @@ -46,4 +48,13 @@ public interface NotificationService {
* Shows a notification asking user to rate this app
*/
void showRateAppNotification();

/**
* Shows a notification asking user to rate this app.
* Same as {@link #showRateAppNotification()} but tries to hint stars automatically recognizing action.
* More about actions see in Manifest for {@link com.adguard.android.contentblocker.receiver.StarsCountReceiver}
* @param action some action from {@link com.adguard.android.contentblocker.receiver.StarsCountReceiver}
* @return count of stars to hint from action (from 1 to 5) or 0 if the action wasn't recognized
*/
int showRateAppNotification(@NonNull String action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,27 @@
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.IdRes;
import androidx.annotation.StringRes;
import androidx.core.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.core.app.NotificationCompat;

import com.adguard.android.contentblocker.R;
import com.adguard.android.contentblocker.receiver.StarsCountReceiver;
import com.adguard.android.contentblocker.ui.MainActivity;
import com.adguard.android.contentblocker.ui.utils.NavigationHelper;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -57,14 +56,16 @@ public class NotificationServiceImpl implements NotificationService {

private static Logger LOG = LoggerFactory.getLogger(NotificationServiceImpl.class);

private static final String STARS_COUNT_ACTION_1 = "count_action_1";
private static final String STARS_COUNT_ACTION_2 = "count_action_2";
private static final String STARS_COUNT_ACTION_3 = "count_action_3";
private static final String STARS_COUNT_ACTION_4 = "count_action_4";
private static final String STARS_COUNT_ACTION_5 = "count_action_5";
/** These actions are the same for {@link StarsCountReceiver}. If you want to change something,
* you don't forget to change the actions in AndroidManifest.xml, also.
* */
private static final String STARS_COUNT_ACTION_1 = "com.adguard.android.contentblocker.count_action_1";
private static final String STARS_COUNT_ACTION_2 = "com.adguard.android.contentblocker.count_action_2";
private static final String STARS_COUNT_ACTION_3 = "com.adguard.android.contentblocker.count_action_3";
private static final String STARS_COUNT_ACTION_4 = "com.adguard.android.contentblocker.count_action_4";
private static final String STARS_COUNT_ACTION_5 = "com.adguard.android.contentblocker.count_action_5";

private static final long REDIRECTION_DELAY = 300;
private static final int RATE_NOTIFICATION_ID = 128;
public static final int RATE_NOTIFICATION_ID = 128;

private final Context context;
private final Handler handler;
Expand All @@ -78,7 +79,6 @@ public NotificationServiceImpl(Context context) {

fillActionsMap();
createNotificationChannel();
registerStarsCountReceiver();
}

@Override
Expand Down Expand Up @@ -109,6 +109,14 @@ public void showRateAppNotification() {
showRateAppNotification(0);
}

@Override
public int showRateAppNotification(@NonNull String action) {
CountId countId = countActions.get(action);
int count = countId == null ? 0 : countId.getCount();
showRateAppNotification(count);
return count;
}

/**
* Initializes the current thread as a looper in case if application was started implicitly in non-UI thread
* This case may occurs after installation via browser menu without app launching.
Expand All @@ -132,17 +140,6 @@ private void fillActionsMap() {
countActions.put(STARS_COUNT_ACTION_5, new CountId(R.id.star5, 5));
}

/**
* Registers {@link StarsCountReceiver} with configured {@link IntentFilter}
*/
private void registerStarsCountReceiver() {
IntentFilter filter = new IntentFilter();
for (String action : countActions.keySet()) {
filter.addAction(action);
}
context.registerReceiver(new StarsCountReceiver(), filter);
}

/**
* Creates {@link NotificationChannel}s for API >= 26
*/
Expand Down Expand Up @@ -243,7 +240,7 @@ private void updateStarsState(int filledCount, RemoteViews remoteViews) {
private RemoteViews createStarsRemoteViews(int filledCount) {
RemoteViews remote = new RemoteViews(context.getPackageName(), R.layout.rate_notification);
for (Map.Entry<String, CountId> entry : countActions.entrySet()) {
Intent countIntent = new Intent(entry.getKey());
Intent countIntent = new Intent(context, StarsCountReceiver.class).setAction(entry.getKey());
remote.setOnClickPendingIntent(entry.getValue().getViewId(), PendingIntent.getBroadcast(context, 0, countIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}

Expand Down Expand Up @@ -282,55 +279,6 @@ private Toast getToast(String message, int duration) {
return toast;
}

/**
* Redirects to Google Play market or to feedback dialog.
* {@link Handler#postDelayed} used to fill selected stars count explicit before redirection
*
* @param count Selected stars count
*/
private void redirectWithDelay(final int count) {
handler.postDelayed(() -> {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (notificationManager != null) {
// Cancel current 'rate app' notification and close notification bar before redirection
notificationManager.cancel(RATE_NOTIFICATION_ID);
Intent closeIntent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(closeIntent);
}

if (count > 3) {
NavigationHelper.redirectToPlayMarket(context);
} else {
Bundle bundle = new Bundle();
bundle.putInt(MainActivity.STARS_COUNT, count);
NavigationHelper.redirectToActivity(context, MainActivity.class, bundle);
}
}, REDIRECTION_DELAY);
}

/**
* Receiver to handle broadcast with 'fill stars` actions
*/
private class StarsCountReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (StringUtils.isEmpty(action)) {
return;
}
CountId countId = countActions.get(action);
if (countId == null) {
return;
}

showRateAppNotification(countId.getCount());
int count = countId.getCount();
redirectWithDelay(count);

}
}

/**
* Wrapper class for view identifier and corresponding filled stars count
*/
Expand Down

0 comments on commit 80b0a3b

Please sign in to comment.