Skip to content

Commit

Permalink
new Google button
Browse files Browse the repository at this point in the history
+ fixed Google search button
+ fixed force floating dialog checkbox
  • Loading branch information
AwaisKing committed Oct 8, 2021
1 parent 48fdbe3 commit a1037d3
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 95 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
minSdk 16
targetSdk sdkVersion

versionCode 118
versionName '11.8'
versionCode 119
versionName '11.9'

multiDexEnabled true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ protected void onCreate(final Bundle savedInstanceState) {
dialogBinding.showFloatingDialog.setEnabled(false);
dialogBinding.showFloatingDialog.setVisibility(View.GONE);
} else {
dialogBinding.showFloatingDialog.setEnabled(true);
dialogBinding.showFloatingDialog.setVisibility(View.VISIBLE);
dialogBinding.showFloatingDialog.setEnabled(showFloating);
dialogBinding.showFloatingDialog.setAlpha(showFloating ? 1f : 0.6f);
dialogBinding.showFloatingDialog.setChecked(showFloatingDialog);
dialogBinding.showFloatingDialog.setVisibility(View.VISIBLE);
}

final int darkMode = SettingsHelper.getNightMode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@

public final class WordDialog extends Dialog implements android.view.View.OnClickListener {
private static final int ALERT_DIALOG_THEME = R.style.DefinitionsDialogTheme;
private static final Exception EMPTY_EXCEPTION = new Exception();

private final String word;
private final Context context;
private final List<?> defs;
private final CustomTabsIntent.Builder customTabsIntent;
private final AdapterClickListener itemClickListener;
private final boolean anySearchAppFound;

private WordDialogBinding wordDialogBinding;

public WordDialog(Context context, final String word, final List<?> defs, final AdapterClickListener itemClickListener) {
super(new ContextThemeWrapper(context, ALERT_DIALOG_THEME), ALERT_DIALOG_THEME);
this.anySearchAppFound = Utils.isAnySearchAppFound(context);
this.context = context;
this.word = word;
this.defs = defs;
Expand Down Expand Up @@ -101,6 +104,7 @@ public void onClick(@NonNull final View v) {

if (v == wordDialogBinding.btnGoogle) {
try {
if (!anySearchAppFound) throw EMPTY_EXCEPTION;
context.startActivity(new Intent(Intent.ACTION_WEB_SEARCH)
.putExtra(SearchManager.QUERY, word)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK));
Expand Down
110 changes: 21 additions & 89 deletions app/src/main/java/awais/backworddictionary/helpers/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.Menu;
import android.view.View;
Expand All @@ -42,8 +38,6 @@
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.appcompat.widget.PopupMenu;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
Expand All @@ -55,6 +49,7 @@
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Locale;

import awais.backworddictionary.BuildConfig;
Expand All @@ -70,11 +65,7 @@ public final class Utils {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

private static final int[][] API_20_BELOW_STATE_DRAWABLES = {
new int[]{-android.R.attr.state_focused},
new int[]{android.R.attr.state_focused},
new int[]{-android.R.attr.state_enabled},
};
private static final Intent SEARCH_QUERY_INTENT = new Intent(Intent.ACTION_WEB_SEARCH);

public static final int[] CUSTOM_TAB_COLORS = new int[]{0xFF4888F2, 0xFF333333, 0xFF3B496B};
public static final String CHARSET = "UTF-8";
Expand Down Expand Up @@ -348,6 +339,24 @@ public static ContextThemeWrapper getStyledContext(final Context context, @Style
return new ContextThemeWrapper(context, style);
}

public static boolean isAnySearchAppFound(@NonNull final Context context) {
final String appID = context.getApplicationContext().getPackageName();
final PackageManager packageManager = context.getPackageManager();

final List<ResolveInfo> pkgAppsList = packageManager.queryIntentActivities(SEARCH_QUERY_INTENT, 0);
int pkgsSize = pkgAppsList.size();
for (int i = pkgsSize - 1; i >= 0; i--) {
final ResolveInfo resolveInfo = pkgAppsList.get(i);
final String strResolveInfo = String.valueOf(resolveInfo);

if (strResolveInfo.contains(appID) && strResolveInfo.contains("TextProcessHelper") ||
resolveInfo != null && resolveInfo.activityInfo != null && appID.equals(resolveInfo.activityInfo.packageName))
--pkgsSize;
}

return pkgsSize > 0;
}

// extracted from String class
public static int indexOfChar(@NonNull final CharSequence sequence, final int ch, final int startIndex) {
final int max = sequence.length();
Expand All @@ -363,81 +372,4 @@ public static int indexOfChar(@NonNull final CharSequence sequence, final int ch
}
return -1;
}

public static Drawable getNumberPickerBackground(Context context) {
if (context == null) return null;

if (!(context instanceof ContextThemeWrapper))
context = new ContextThemeWrapper(context, R.style.DefinitionsDialogTheme);

final Resources.Theme theme = context.getTheme();
final Resources resources = context.getResources();
final TypedValue outValue = new TypedValue();

Drawable drawable;
try {
drawable = ResourcesCompat.getDrawable(resources, R.drawable.mtrl_background_outlined, theme);
} catch (final Exception ex) {
try {
drawable = ContextCompat.getDrawable(context, R.drawable.mtrl_background_outlined);
} catch (final Exception e) {
drawable = null;
}
}

if (drawable == null) {
theme.resolveAttribute(R.attr.colorControlNormal, outValue, true);
final int colorControlNormal = outValue.resourceId != 0 ?
ResourcesCompat.getColor(resources, outValue.resourceId, theme) : outValue.data;

theme.resolveAttribute(R.attr.colorControlActivated, outValue, true);
final int colorControlActivated = outValue.resourceId != 0 ?
ResourcesCompat.getColor(resources, outValue.resourceId, theme) : outValue.data;

theme.resolveAttribute(R.attr.colorControlHighlight, outValue, true);
final int colorControlHighlight = outValue.resourceId != 0 ?
ResourcesCompat.getColor(resources, outValue.resourceId, theme) : outValue.data;

final float density = context.getResources().getDisplayMetrics().density;
final float cornerRadius = density * 4f;
final int strokeSize = Math.round(density * 0.99f);

final StateListDrawable stateListDrawable = new StateListDrawable();
GradientDrawable gradientDrawable;

// state_focused="false"
{
gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.TRANSPARENT);
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(cornerRadius);
gradientDrawable.setStroke(strokeSize, colorControlNormal);
stateListDrawable.addState(API_20_BELOW_STATE_DRAWABLES[0], gradientDrawable);
}

// state_focused="true"
{
gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.TRANSPARENT);
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(cornerRadius);
gradientDrawable.setStroke(strokeSize, colorControlActivated);
stateListDrawable.addState(API_20_BELOW_STATE_DRAWABLES[1], gradientDrawable);
}

// state_enabled="false"
{
gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.TRANSPARENT);
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(cornerRadius);
gradientDrawable.setStroke(strokeSize, colorControlHighlight);
stateListDrawable.addState(API_20_BELOW_STATE_DRAWABLES[2], gradientDrawable);
}

drawable = stateListDrawable;
}

return drawable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
Expand All @@ -24,6 +27,8 @@
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

Expand All @@ -50,8 +55,13 @@ public final class HorizontalNumberPicker extends LinearLayoutCompat implements
private static final int DEFAULT_MIN_VALUE = 1;
private static final long LONG_TAP_TIMEOUT = 300L;

private static final int[][] API_20_BELOW_STATE_DRAWABLES = {
new int[]{-android.R.attr.state_focused},
new int[]{android.R.attr.state_focused},
new int[]{-android.R.attr.state_enabled},
};
private static final int[] FOCUSED_STATE_ARRAY = {android.R.attr.state_focused};
private static final int[] UNFOCUSED_STATE_ARRAY = {0, -android.R.attr.state_focused};
private static final int[] UNFOCUSED_STATE_ARRAY = {-android.R.attr.state_focused}; // todo was 0, -focused

private final ExponentialTracker tracker;
private final UIGestureRecognizerDelegate delegate = new UIGestureRecognizerDelegate();
Expand Down Expand Up @@ -85,7 +95,7 @@ public HorizontalNumberPicker(Context context, final AttributeSet attrs, final i
setGravity(Gravity.CENTER);

if (isInEditMode()) setBackgroundResource(R.drawable.mtrl_background_outlined);
else setBackground(Utils.getNumberPickerBackground(context));
else setBackground(getNumberPickerBackground(context));

final TypedValue outValue = new TypedValue();
theme.resolveAttribute(R.attr.selectableItemBackgroundBorderless, outValue, true);
Expand Down Expand Up @@ -346,4 +356,81 @@ public void afterTextChanged(final Editable s) { }

@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) { }

private static final Drawable getNumberPickerBackground(Context context) {
if (context == null) return null;

if (!(context instanceof ContextThemeWrapper))
context = new ContextThemeWrapper(context, R.style.DefinitionsDialogTheme);

final Resources.Theme theme = context.getTheme();
final Resources resources = context.getResources();
final TypedValue outValue = new TypedValue();

Drawable drawable;
try {
drawable = ResourcesCompat.getDrawable(resources, R.drawable.mtrl_background_outlined, theme);
} catch (final Exception ex) {
try {
drawable = ContextCompat.getDrawable(context, R.drawable.mtrl_background_outlined);
} catch (final Exception e) {
drawable = null;
}
}

if (drawable == null) {
theme.resolveAttribute(R.attr.colorControlNormal, outValue, true);
final int colorControlNormal = outValue.resourceId != 0 ?
ResourcesCompat.getColor(resources, outValue.resourceId, theme) : outValue.data;

theme.resolveAttribute(R.attr.colorControlActivated, outValue, true);
final int colorControlActivated = outValue.resourceId != 0 ?
ResourcesCompat.getColor(resources, outValue.resourceId, theme) : outValue.data;

theme.resolveAttribute(R.attr.colorControlHighlight, outValue, true);
final int colorControlHighlight = outValue.resourceId != 0 ?
ResourcesCompat.getColor(resources, outValue.resourceId, theme) : outValue.data;

final float density = context.getResources().getDisplayMetrics().density;
final float cornerRadius = density * 4f;
final int strokeSize = Math.round(density * 0.99f);

final StateListDrawable stateListDrawable = new StateListDrawable();
GradientDrawable gradientDrawable;

// state_focused="false"
{
gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.TRANSPARENT);
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(cornerRadius);
gradientDrawable.setStroke(strokeSize, colorControlNormal);
stateListDrawable.addState(API_20_BELOW_STATE_DRAWABLES[0], gradientDrawable);
}

// state_focused="true"
{
gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.TRANSPARENT);
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(cornerRadius);
gradientDrawable.setStroke(strokeSize, colorControlActivated);
stateListDrawable.addState(API_20_BELOW_STATE_DRAWABLES[1], gradientDrawable);
}

// state_enabled="false"
{
gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.TRANSPARENT);
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(cornerRadius);
gradientDrawable.setStroke(strokeSize, colorControlHighlight);
stateListDrawable.addState(API_20_BELOW_STATE_DRAWABLES[2], gradientDrawable);
}

drawable = stateListDrawable;
}

return drawable;
}
}

0 comments on commit a1037d3

Please sign in to comment.