UntrackMe-app-android-redir.../app/src/main/java/app/fedilab/nitterizeme/activities/AppsPickerActivity.java

239 lines
10 KiB
Java
Raw Normal View History

2020-04-19 17:31:07 +02:00
package app.fedilab.nitterizeme.activities;
/* 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 <http://www.gnu.org/licenses>. */
2022-05-15 11:30:23 +02:00
import static app.fedilab.nitterizeme.activities.MainActivity.APP_PREFS;
import static app.fedilab.nitterizeme.helpers.Utils.INTENT_ACTION;
import static app.fedilab.nitterizeme.helpers.Utils.KILL_ACTIVITY;
import static app.fedilab.nitterizeme.helpers.Utils.LAST_USED_APP_PACKAGE;
import static app.fedilab.nitterizeme.helpers.Utils.URL_APP_PICKER;
2020-04-19 17:31:07 +02:00
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
2020-04-19 17:31:07 +02:00
import android.content.Intent;
2020-06-05 11:26:05 +02:00
import android.content.SharedPreferences;
2020-04-19 17:31:07 +02:00
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.sqlite.SQLiteDatabase;
2020-04-19 17:31:07 +02:00
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
2020-04-19 17:31:07 +02:00
import java.util.ArrayList;
import java.util.List;
import app.fedilab.nitterizeme.R;
import app.fedilab.nitterizeme.adapters.AppPickerAdapter;
2020-12-01 10:29:24 +01:00
import app.fedilab.nitterizeme.databinding.ActivityPickupAppBinding;
2020-04-19 17:31:07 +02:00
import app.fedilab.nitterizeme.entities.AppPicker;
import app.fedilab.nitterizeme.helpers.Utils;
import app.fedilab.nitterizeme.sqlite.DefaultAppDAO;
import app.fedilab.nitterizeme.sqlite.Sqlite;
2020-04-19 17:31:07 +02:00
public class AppsPickerActivity extends Activity {
private String url;
2020-04-20 19:28:00 +02:00
private String action;
private String appToUse;
private String appName;
2020-04-19 17:31:07 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2020-12-01 10:29:24 +01:00
ActivityPickupAppBinding binding = ActivityPickupAppBinding.inflate(getLayoutInflater());
View viewRoot = binding.getRoot();
setContentView(viewRoot);
2020-04-19 17:31:07 +02:00
if (getIntent() == null) {
finish();
}
Bundle b = getIntent().getExtras();
if (b == null) {
finish();
}
if (b != null) {
url = b.getString(URL_APP_PICKER, null);
2020-04-20 19:28:00 +02:00
action = b.getString(INTENT_ACTION, null);
2020-04-19 17:31:07 +02:00
}
2020-04-20 19:28:00 +02:00
if (url == null || action == null) {
2020-04-19 17:31:07 +02:00
finish();
}
//At this point we are sure that url is not null
Intent stopMainActivity = new Intent(KILL_ACTIVITY);
sendBroadcast(stopMainActivity);
2020-04-20 19:28:00 +02:00
Intent delegate = new Intent(action);
2020-04-19 17:31:07 +02:00
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2020-04-20 19:28:00 +02:00
if (action.compareTo(Intent.ACTION_VIEW) == 0) {
delegate.setData(Uri.parse(url));
} else {
delegate.putExtra(Intent.EXTRA_TEXT, url);
delegate.setType("text/plain");
}
2020-05-06 18:28:17 +02:00
List<ResolveInfo> activities;
2020-11-29 17:50:06 +01:00
2020-05-06 18:28:17 +02:00
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);
}
2020-04-20 19:28:00 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2020-12-01 10:29:24 +01:00
binding.blank.setOnClickListener(v -> finish());
2020-04-19 17:31:07 +02:00
String thisPackageName = getApplicationContext().getPackageName();
ArrayList<String> packages = new ArrayList<>();
List<AppPicker> appPickers = new ArrayList<>();
2020-06-05 11:26:05 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE);
String last_used_app = sharedpreferences.getString(LAST_USED_APP_PACKAGE, null);
int i = 0;
2020-04-19 17:31:07 +02:00
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);
2020-06-05 11:26:05 +02:00
if (i == 0 && last_used_app == null) {
appPicker.setSelected(true);
appToUse = packageName;
appName = String.valueOf(currentInfo.loadLabel(getPackageManager()));
} else if (last_used_app != null && last_used_app.compareTo(packageName) == 0) {
appPicker.setSelected(true);
appToUse = packageName;
appName = String.valueOf(currentInfo.loadLabel(getPackageManager()));
}
appPickers.add(appPicker);
packages.add(packageName);
2020-04-20 18:12:51 +02:00
i++;
}
}
String defaultApp = new DefaultAppDAO(AppsPickerActivity.this, db).getDefault(packages);
2020-12-01 10:29:24 +01:00
binding.url.setText(url);
2020-04-20 17:56:49 +02:00
if (defaultApp != null) {
2020-04-20 19:28:00 +02:00
Intent intent = new Intent(action, Uri.parse(url));
intent.setPackage(defaultApp);
startActivity(intent);
finish();
2020-07-10 11:43:53 +02:00
return;
} else {
2020-12-01 10:29:24 +01:00
binding.appContainer.setVisibility(View.VISIBLE);
AppPickerAdapter appPickerAdapter = new AppPickerAdapter(appPickers);
2020-12-01 10:29:24 +01:00
binding.appList.setAdapter(appPickerAdapter);
binding.appList.setNumColumns(3);
binding.appList.setOnItemClickListener((parent, view1, position, id) -> {
2020-05-06 18:28:17 +02:00
if (!appPickers.get(position).isSelected()) {
for (AppPicker ap : appPickers) {
ap.setSelected(false);
}
appPickers.get(position).setSelected(true);
appToUse = appPickers.get(position).getPackageName();
appName = appPickers.get(position).getName();
appPickerAdapter.notifyDataSetChanged();
} else {
2020-06-05 11:26:05 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(LAST_USED_APP_PACKAGE, appToUse);
editor.apply();
2020-05-06 18:28:17 +02:00
if (action.compareTo(Intent.ACTION_VIEW) == 0) {
Intent intent = new Intent(action, Uri.parse(url));
intent.setPackage(appToUse);
startActivity(intent);
} else {
Intent intent = new Intent(action);
intent.putExtra(Intent.EXTRA_TEXT, url);
intent.setType("text/plain");
intent.setPackage(appToUse);
startActivity(intent);
}
finish();
}
2020-05-06 18:28:17 +02:00
});
2020-12-01 10:29:24 +01:00
binding.always.setOnClickListener(v -> {
boolean isPresent = new DefaultAppDAO(AppsPickerActivity.this, db).isPresent(appToUse);
long val = -1;
if (isPresent) {
ArrayList<String> oldConcurrent = new DefaultAppDAO(AppsPickerActivity.this, db).getConcurrent(appToUse);
2022-05-15 11:30:23 +02:00
ArrayList<String> newConcurrent = oldConcurrent != null ? Utils.union(oldConcurrent, packages) : packages;
2020-07-10 11:43:53 +02:00
newConcurrent.remove(appToUse);
new DefaultAppDAO(AppsPickerActivity.this, db).update(appToUse, newConcurrent);
} else {
val = new DefaultAppDAO(AppsPickerActivity.this, db).insert(appToUse, packages);
}
if (val > 0) {
Toast.makeText(AppsPickerActivity.this, getString(R.string.default_app_indication, appName), Toast.LENGTH_LONG).show();
}
2020-04-20 19:28:00 +02:00
if (action.compareTo(Intent.ACTION_VIEW) == 0) {
Intent intent = new Intent(action, Uri.parse(url));
intent.setPackage(appToUse);
startActivity(intent);
} else {
Intent intent = new Intent(action);
intent.putExtra(Intent.EXTRA_TEXT, url);
intent.setType("text/plain");
intent.setPackage(appToUse);
startActivity(intent);
}
finish();
});
2020-12-01 10:29:24 +01:00
binding.once.setOnClickListener(v -> {
2020-04-20 19:28:00 +02:00
if (action.compareTo(Intent.ACTION_VIEW) == 0) {
Intent intent = new Intent(action, Uri.parse(url));
intent.setPackage(appToUse);
startActivity(intent);
} else {
Intent intent = new Intent(action);
intent.putExtra(Intent.EXTRA_TEXT, url);
intent.setType("text/plain");
intent.setPackage(appToUse);
startActivity(intent);
}
2020-06-05 11:26:05 +02:00
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(LAST_USED_APP_PACKAGE, appToUse);
editor.apply();
finish();
});
}
2020-12-01 10:29:24 +01:00
binding.copyLink.setOnClickListener(v -> {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("", url);
2020-05-30 17:44:30 +02:00
assert clipboard != null;
clipboard.setPrimaryClip(clipData);
Toast.makeText(this, getString(R.string.copy_done), Toast.LENGTH_SHORT).show();
});
2020-04-19 17:31:07 +02:00
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}