From 4d668273f7abf835ffb4437f76cc0893547fc817 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 19 Apr 2020 17:31:07 +0200 Subject: [PATCH] allow to pickup apps --- app/src/main/AndroidManifest.xml | 10 +- .../activities/AppsPickerActivity.java | 120 ++++++++++++++++++ .../activities/TransformActivity.java | 21 +-- .../adapters/AppPickerAdapter.java | 100 +++++++++++++++ .../nitterizeme/entities/AppPicker.java | 57 +++++++++ .../fedilab/nitterizeme/helpers/Utils.java | 1 + .../drawable-v24/ic_launcher_foreground.xml | 34 ----- app/src/main/res/drawable/ic_app_logo.xml | 20 +++ .../main/res/drawable/rounded_selector.xml | 7 + .../main/res/layout/activity_pickup_app.xml | 90 +++++++++++++ app/src/main/res/layout/drawer_app_picker.xml | 32 +++++ app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 1 + 13 files changed, 449 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/app/fedilab/nitterizeme/activities/AppsPickerActivity.java create mode 100644 app/src/main/java/app/fedilab/nitterizeme/adapters/AppPickerAdapter.java create mode 100644 app/src/main/java/app/fedilab/nitterizeme/entities/AppPicker.java delete mode 100644 app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/ic_app_logo.xml create mode 100644 app/src/main/res/drawable/rounded_selector.xml create mode 100644 app/src/main/res/layout/activity_pickup_app.xml create mode 100644 app/src/main/res/layout/drawer_app_picker.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 36c855e..189e65f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -54,11 +54,13 @@ - - + . */ + +import android.app.Activity; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.net.Uri; +import android.os.Bundle; +import android.util.Log; +import android.widget.GridView; +import android.widget.RelativeLayout; + +import java.util.ArrayList; +import java.util.List; + +import app.fedilab.nitterizeme.R; +import app.fedilab.nitterizeme.adapters.AppPickerAdapter; +import app.fedilab.nitterizeme.entities.AppPicker; + +import static app.fedilab.nitterizeme.helpers.Utils.KILL_ACTIVITY; +import static app.fedilab.nitterizeme.helpers.Utils.URL_APP_PICKER; + + +public class AppsPickerActivity extends Activity { + + + private GridView gridView; + private String url; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_pickup_app); + SharedPreferences sharedpreferences = getSharedPreferences(MainActivity.APP_PREFS, Context.MODE_PRIVATE); + if (getIntent() == null) { + finish(); + } + Bundle b = getIntent().getExtras(); + if (b == null) { + finish(); + } + if (b != null) { + url = b.getString(URL_APP_PICKER, null); + } + if (url == null) { + finish(); + } + //At this point we are sure that url is not null + Intent stopMainActivity = new Intent(KILL_ACTIVITY); + sendBroadcast(stopMainActivity); + + Intent delegate = new Intent(Intent.ACTION_VIEW); + delegate.setDataAndType(Uri.parse(url), "text/html"); + delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + + List activities; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { + activities = getPackageManager().queryIntentActivities(delegate, PackageManager.MATCH_ALL); + } else { + activities = getPackageManager().queryIntentActivities(delegate, 0); + } + + + RelativeLayout blank = findViewById(R.id.blank); + blank.setOnClickListener(v -> finish()); + + String thisPackageName = getApplicationContext().getPackageName(); + ArrayList packages = new ArrayList<>(); + List appPickers = new ArrayList<>(); + for (ResolveInfo currentInfo : activities) { + String packageName = currentInfo.activityInfo.packageName; + if (!thisPackageName.equals(packageName) && !packages.contains(packageName)) { + AppPicker appPicker = new AppPicker(); + appPicker.setIcon(currentInfo.activityInfo.loadIcon(getPackageManager())); + appPicker.setName(String.valueOf(currentInfo.loadLabel(getPackageManager()))); + appPicker.setPackageName(packageName); + appPickers.add(appPicker); + } + } + + + GridView gridView = findViewById(R.id.app_list); + AppPickerAdapter appPickerAdapter = new AppPickerAdapter(appPickers); + gridView.setAdapter(appPickerAdapter); + gridView.setNumColumns(3); + gridView.setOnItemClickListener((parent, view1, position, id) -> { + for(AppPicker ap: appPickers){ + ap.setSelected(false); + } + appPickers.get(position).setSelected(true); + appPickerAdapter.notifyDataSetChanged(); + }); + + } + + @Override + protected void onDestroy() { + + super.onDestroy(); + } + +} diff --git a/app/src/main/java/app/fedilab/nitterizeme/activities/TransformActivity.java b/app/src/main/java/app/fedilab/nitterizeme/activities/TransformActivity.java index 6dcd837..01accc7 100644 --- a/app/src/main/java/app/fedilab/nitterizeme/activities/TransformActivity.java +++ b/app/src/main/java/app/fedilab/nitterizeme/activities/TransformActivity.java @@ -15,18 +15,15 @@ package app.fedilab.nitterizeme.activities; * see . */ import android.app.Activity; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; -import android.os.Parcelable; import android.util.Patterns; import android.view.View; import android.widget.Button; @@ -35,7 +32,6 @@ import android.widget.RelativeLayout; import android.widget.TextView; import androidx.appcompat.app.AlertDialog; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; @@ -43,7 +39,6 @@ import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; -import java.util.List; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -59,10 +54,10 @@ import static app.fedilab.nitterizeme.activities.CheckAppActivity.shortener_doma import static app.fedilab.nitterizeme.activities.CheckAppActivity.twitter_domains; import static app.fedilab.nitterizeme.activities.CheckAppActivity.youtube_domains; import static app.fedilab.nitterizeme.activities.MainActivity.SET_BIBLIOGRAM_ENABLED; -import static app.fedilab.nitterizeme.activities.MainActivity.SET_EMBEDDED_PLAYER; import static app.fedilab.nitterizeme.activities.MainActivity.SET_INVIDIOUS_ENABLED; import static app.fedilab.nitterizeme.activities.MainActivity.SET_NITTER_ENABLED; import static app.fedilab.nitterizeme.helpers.Utils.KILL_ACTIVITY; +import static app.fedilab.nitterizeme.helpers.Utils.URL_APP_PICKER; import static app.fedilab.nitterizeme.helpers.Utils.ampExtract; import static app.fedilab.nitterizeme.helpers.Utils.bibliogramAccountPattern; import static app.fedilab.nitterizeme.helpers.Utils.bibliogramPostPattern; @@ -332,7 +327,15 @@ public class TransformActivity extends Activity { * @param i original intent */ private void forwardToBrowser(Intent i) { - Intent intent = new Intent(); + + Intent app_picker = new Intent(TransformActivity.this, AppsPickerActivity.class); + Bundle b = new Bundle(); + b.putString(URL_APP_PICKER, i.getDataString()); + app_picker.putExtras(b); + startActivity(app_picker); + finish(); + + /* Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); String type = i.getType(); if (type == null) { @@ -390,7 +393,7 @@ public class TransformActivity extends Activity { chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{})); startActivity(chooserIntent); } - finish(); + finish();*/ } @@ -607,7 +610,7 @@ public class TransformActivity extends Activity { }; thread.start(); return; - }else { + } else { newUrl = remove_tracking_param(url); } if (newUrl != null) { diff --git a/app/src/main/java/app/fedilab/nitterizeme/adapters/AppPickerAdapter.java b/app/src/main/java/app/fedilab/nitterizeme/adapters/AppPickerAdapter.java new file mode 100644 index 0000000..0116110 --- /dev/null +++ b/app/src/main/java/app/fedilab/nitterizeme/adapters/AppPickerAdapter.java @@ -0,0 +1,100 @@ +package app.fedilab.nitterizeme.adapters; +/* Copyright 2020 Thomas Schneider + * + * This file is a part of UntrackMe + * + * This program 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. + * + * UntrackMe 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 UntrackMe; if not, + * see . */ + +import android.content.res.Resources; +import android.os.Build; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.constraintlayout.widget.ConstraintLayout; + +import java.util.List; + +import app.fedilab.nitterizeme.R; +import app.fedilab.nitterizeme.entities.AppPicker; + +public class AppPickerAdapter extends BaseAdapter { + + private List appPickers; + + public AppPickerAdapter(List appPickers) { + this.appPickers = appPickers; + } + + + @Override + public int getCount() { + return appPickers.size(); + } + + @Override + public AppPicker getItem(int position) { + return appPickers.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + + final ViewHolder holder; + AppPicker appPicker = appPickers.get(position); + if (convertView == null) { + LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); + convertView = layoutInflater.inflate(R.layout.drawer_app_picker, parent, false); + holder = new ViewHolder(); + holder.app_icon = convertView.findViewById(R.id.app_icon); + holder.app_name = convertView.findViewById(R.id.app_name); + holder.app_container = convertView.findViewById(R.id.app_container); + convertView.setTag(holder); + } else { + holder = (ViewHolder) convertView.getTag(); + } + + try { + holder.app_icon.setImageDrawable(appPicker.getIcon()); + } catch (Resources.NotFoundException e) { + holder.app_icon.setImageResource(R.drawable.ic_android); + } + holder.app_name.setText(appPicker.getName()); + + + if( appPicker.isSelected()) { + holder.app_container.setBackgroundResource(R.drawable.rounded_selector); + }else{ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + holder.app_container.setBackground(null); + } + } + + return convertView; + + } + + private static class ViewHolder { + ImageView app_icon; + TextView app_name; + ConstraintLayout app_container; + } + +} \ No newline at end of file diff --git a/app/src/main/java/app/fedilab/nitterizeme/entities/AppPicker.java b/app/src/main/java/app/fedilab/nitterizeme/entities/AppPicker.java new file mode 100644 index 0000000..71041e4 --- /dev/null +++ b/app/src/main/java/app/fedilab/nitterizeme/entities/AppPicker.java @@ -0,0 +1,57 @@ +package app.fedilab.nitterizeme.entities; + +import android.graphics.drawable.Drawable; + +/* Copyright 2020 Thomas Schneider + * + * This file is a part of UntrackMe + * + * This program 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. + * + * UntrackMe 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 UntrackMe; if not, + * see . */ +public class AppPicker { + + private Drawable icon; + private String name; + private String packageName; + private boolean selected; + + public Drawable getIcon() { + return icon; + } + + public void setIcon(Drawable icon) { + this.icon = icon; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public boolean isSelected() { + return selected; + } + + public void setSelected(boolean selected) { + this.selected = selected; + } +} diff --git a/app/src/main/java/app/fedilab/nitterizeme/helpers/Utils.java b/app/src/main/java/app/fedilab/nitterizeme/helpers/Utils.java index 9f656a2..7ace71a 100644 --- a/app/src/main/java/app/fedilab/nitterizeme/helpers/Utils.java +++ b/app/src/main/java/app/fedilab/nitterizeme/helpers/Utils.java @@ -54,6 +54,7 @@ import static app.fedilab.nitterizeme.activities.MainActivity.SET_NITTER_ENABLED public class Utils { public static final String KILL_ACTIVITY = "kill_activity"; + public static final String URL_APP_PICKER = "url_app_picker"; public static final Pattern youtubePattern = Pattern.compile("(www\\.|m\\.)?(youtube\\.com|youtu\\.be|youtube-nocookie\\.com)/(((?!([\"'<])).)*)"); public static final Pattern nitterPattern = Pattern.compile("(mobile\\.|www\\.)?twitter.com([\\w-/]+)"); public static final Pattern bibliogramPostPattern = Pattern.compile("(m\\.|www\\.)?instagram.com(/p/[\\w-/]+)"); diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml deleted file mode 100644 index 1f6bb29..0000000 --- a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - - - - - diff --git a/app/src/main/res/drawable/ic_app_logo.xml b/app/src/main/res/drawable/ic_app_logo.xml new file mode 100644 index 0000000..04ee601 --- /dev/null +++ b/app/src/main/res/drawable/ic_app_logo.xml @@ -0,0 +1,20 @@ + + + + + + + diff --git a/app/src/main/res/drawable/rounded_selector.xml b/app/src/main/res/drawable/rounded_selector.xml new file mode 100644 index 0000000..f8f3c1b --- /dev/null +++ b/app/src/main/res/drawable/rounded_selector.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_pickup_app.xml b/app/src/main/res/layout/activity_pickup_app.xml new file mode 100644 index 0000000..97a09fb --- /dev/null +++ b/app/src/main/res/layout/activity_pickup_app.xml @@ -0,0 +1,90 @@ + + + + + + +