Fix browser opening; update some tests (#1203)

Closes https://codeberg.org/gitnex/GitNex/issues/1200

Co-authored-by: qwerty287 <ndev@web.de>
Co-authored-by: M M Arif <mmarif@swatian.com>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1203
Reviewed-by: M M Arif <mmarif@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
This commit is contained in:
qwerty287 2022-09-07 08:25:55 +02:00 committed by M M Arif
parent beac76e6c0
commit 9d6ef9e079
4 changed files with 81 additions and 10 deletions

View File

@ -201,4 +201,12 @@
</activity>
</application>
<queries>
<intent>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="https" />
</intent>
</queries>
</manifest>

View File

@ -2,8 +2,10 @@ package org.mian.gitnex.helpers;
import android.app.Activity;
import android.content.*;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Typeface;
@ -11,6 +13,7 @@ import android.net.Uri;
import android.os.Build;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import androidx.annotation.ColorInt;
@ -370,19 +373,66 @@ public class AppUtil {
ctx.startActivity(Intent.createChooser(sharingIntent, url));
}
private static Intent wrapBrowserIntent(Context context, Intent intent) {
final PackageManager pm = context.getPackageManager();
final List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(intent).setData(intent.getData().buildUpon().authority("example.com").scheme("https").build()), PackageManager.MATCH_ALL);
final ArrayList<Intent> chooserIntents = new ArrayList<>();
final String ourPackageName = context.getPackageName();
Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm));
for(ResolveInfo resInfo : activities) {
ActivityInfo info = resInfo.activityInfo;
if(!info.enabled || !info.exported) {
continue;
}
if(info.packageName.equals(ourPackageName)) {
continue;
}
Intent targetIntent = new Intent(intent);
targetIntent.setPackage(info.packageName);
targetIntent.setDataAndType(intent.getData(), intent.getType());
chooserIntents.add(targetIntent);
}
if(chooserIntents.isEmpty()) {
return null;
}
final Intent lastIntent = chooserIntents.remove(chooserIntents.size() - 1);
if(chooserIntents.isEmpty()) {
return lastIntent;
}
Intent chooserIntent = Intent.createChooser(lastIntent, null);
String extraName = Intent.EXTRA_ALTERNATE_INTENTS;
chooserIntent.putExtra(extraName, chooserIntents.toArray(new Intent[0]));
return chooserIntent;
}
public static void openUrlInBrowser(Context context, String url) {
TinyDB tinyDB = TinyDB.getInstance(context);
Intent i;
if(tinyDB.getBoolean("useCustomTabs")) {
i = new CustomTabsIntent.Builder().setDefaultColorSchemeParams(
new CustomTabColorSchemeParams.Builder().setToolbarColor(getColorFromAttribute(context, R.attr.primaryBackgroundColor)).setNavigationBarColor(getColorFromAttribute(context, R.attr.primaryBackgroundColor))
.setSecondaryToolbarColor(R.attr.primaryTextColor).build()).build().intent;
i.setData(Uri.parse(url));
}
else {
i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
i.addCategory(Intent.CATEGORY_BROWSABLE);
}
try {
if(tinyDB.getBoolean("useCustomTabs")) {
new CustomTabsIntent.Builder().setDefaultColorSchemeParams(new CustomTabColorSchemeParams.Builder().setToolbarColor(getColorFromAttribute(context, R.attr.primaryBackgroundColor))
.setNavigationBarColor(getColorFromAttribute(context, R.attr.primaryBackgroundColor)).setSecondaryToolbarColor(R.attr.primaryTextColor).build()).build().launchUrl(context, Uri.parse(url));
}
else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
i.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(i);
Intent browserIntent = wrapBrowserIntent(context, i);
if(browserIntent == null) {
Toasty.error(context, context.getString(R.string.genericError));
}
context.startActivity(browserIntent);
}
catch(ActivityNotFoundException e) {
Toasty.error(context, context.getString(R.string.browserOpenFailed));

View File

@ -19,9 +19,12 @@ public class ParseDiffTest {
List<FileDiffView> parsed = ParseDiff.getFileDiffViewArray(commitDiff);
assertEquals(4, parsed.size());
assertTrue(parsed.get(0).isFileBinary());
assertEquals("binary ", parsed.get(0).getFileInfo());
assertFalse(parsed.get(1).isFileBinary());
assertEquals(1, parsed.get(1).getFileContents().size());
assertEquals("newOne.txt", parsed.get(1).getFileName());
assertEquals("+2, -0", parsed.get(1).getFileInfo());
assertEquals(2, parsed.get(1).getFileContents().get(0).getLineAdded());
assertEquals(0, parsed.get(1).getFileContents().get(0).getLineRemoved());

View File

@ -1,8 +1,8 @@
package org.mian.gitnex.helpers;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.function.ThrowingRunnable;
import static org.junit.Assert.*;
/**
* @author 6543
@ -10,6 +10,11 @@ import static org.junit.Assert.assertTrue;
public class VersionTest {
@Test
public void invalid() {
assertThrows(IllegalArgumentException.class, () -> new Version("abcd"));
}
@Test
public void equal() {
@ -21,6 +26,7 @@ public class VersionTest {
assertTrue(new Version("v1.12.0").equal("1.12.0"));
assertTrue(new Version("0").equal("0"));
assertTrue(new Version("").equal("0"));
assertTrue(new Version("main").equal("main"));
assertFalse(new Version("1.12.1").equal("1.12.0+dev-211-g316db0fe7"));
assertFalse(new Version("v1.12.0").equal("1.10.0"));
@ -30,6 +36,7 @@ public class VersionTest {
assertFalse(new Version("1.2").equal("2.1"));
assertFalse(new Version("2.2").equal("2.1.120"));
assertFalse(new Version("1.12.3").equal("1.13.0+dev-307-g633f52c22"));
assertFalse(new Version("main").equal("1.17.1"));
}
@ -89,6 +96,7 @@ public class VersionTest {
assertTrue(new Version("1.13.0+dev-30-gb02d2c377").higher("1.11.4"));
assertTrue(new Version("2.1").higher("1.2"));
assertTrue(new Version("1.13.0+dev-307-g633f52c22").higher("1.12.3"));
assertTrue(new Version("main").higher("1.18.0"));
assertFalse(new Version("1").higher("1.1.10"));
assertFalse(new Version("1.12.0+dev-211-g316db0fe7").higher("1.12.1"));
@ -97,6 +105,8 @@ public class VersionTest {
assertFalse(new Version("v1.12").higher("2.12.0"));
assertFalse(new Version("1").higher("2"));
assertFalse(new Version("2.1.120").higher("2.2"));
assertFalse(new Version("main").higher("main"));
assertFalse(new Version("1.17.0").higher("main"));
}