Remove map from id to link. make link handler more solid

This commit is contained in:
Pfaffenrodt
2022-10-30 21:04:35 +01:00
parent 1aafcdc6ae
commit 6f777068ab
3 changed files with 43 additions and 47 deletions

View File

@@ -27,12 +27,19 @@ public class AboutActivity extends CatimaAppCompatActivity {
setSupportActionBar(binding.toolbar);
enableToolbarBackButton();
TextView copyright = binding.creditsSub;
copyright.setText(content.getCopyright());
TextView versionHistory = binding.versionHistorySub;
versionHistory.setText(content.getVersionHistory());
binding.versionHistory.setTag("https://catima.app/changelog/");
binding.translate.setTag("https://hosted.weblate.org/engage/catima/");
binding.license.setTag("https://github.com/CatimaLoyalty/Android/blob/master/LICENSE");
binding.repo.setTag("https://github.com/CatimaLoyalty/Android/");
binding.privacy.setTag("https://catima.app/privacy-policy/");
binding.reportError.setTag("https://github.com/CatimaLoyalty/Android/issues");
binding.rate.setTag("https://play.google.com/store/apps/details?id=me.hackerchick.catima");
bindClickListeners();
}
@@ -54,8 +61,12 @@ public class AboutActivity extends CatimaAppCompatActivity {
}
private void bindClickListeners() {
View.OnClickListener openExternalBrowser = view -> (new AboutOpenLinkHandler()).open(this, view);
View.OnClickListener openExternalBrowser = view -> {
Object tag = view.getTag();
if (tag instanceof String && ((String) tag).startsWith("https://")) {
(new OpenWebLinkHandler()).open(this, (String) tag);
}
};
binding.versionHistory.setOnClickListener(openExternalBrowser);
binding.translate.setOnClickListener(openExternalBrowser);
binding.license.setOnClickListener(openExternalBrowser);

View File

@@ -1,44 +0,0 @@
package protect.card_locker;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.collection.ArrayMap;
public class AboutOpenLinkHandler {
private static final String TAG = "Catima";
private final ArrayMap<Integer, String> viewIdToUrlMap = new ArrayMap<Integer, String>();
AboutOpenLinkHandler() {
viewIdToUrlMap.put(R.id.version_history, "https://catima.app/changelog/");
viewIdToUrlMap.put(R.id.translate, "https://hosted.weblate.org/engage/catima/");
viewIdToUrlMap.put(R.id.license, "https://github.com/CatimaLoyalty/Android/blob/master/LICENSE");
viewIdToUrlMap.put(R.id.repo, "https://github.com/CatimaLoyalty/Android/");
viewIdToUrlMap.put(R.id.privacy, "https://catima.app/privacy-policy/");
viewIdToUrlMap.put(R.id.report_error, "https://github.com/CatimaLoyalty/Android/issues");
viewIdToUrlMap.put(R.id.rate, "https://play.google.com/store/apps/details?id=me.hackerchick.catima");
}
public void open(AppCompatActivity activity, View view) {
String url = viewIdToUrlMap.get(view.getId());
if (url == null) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
try {
activity.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, R.string.failedToOpenUrl, Toast.LENGTH_LONG).show();
Log.e(TAG, "No activity found to handle intent", e);
}
}
}

View File

@@ -0,0 +1,29 @@
package protect.card_locker;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class OpenWebLinkHandler {
private static final String TAG = "Catima";
public void open(AppCompatActivity activity, String url) {
if (url == null) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
try {
activity.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, R.string.failedToOpenUrl, Toast.LENGTH_LONG).show();
Log.e(TAG, "No activity found to handle intent", e);
}
}
}