Adding auto open urls in app.

This commit is contained in:
Sergiotarxz 2023-08-30 16:52:59 +02:00
parent 6e16a093e8
commit 7ef88c7ab6
5 changed files with 104 additions and 26 deletions

View File

@ -32,8 +32,6 @@ android {
}
dependencies {
val core_version = "1.10.1"
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
@ -44,3 +42,11 @@ dependencies {
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
}
allprojects {
gradle.projectsEvaluated {
tasks.withType<JavaCompile> {
options.compilerArgs.add("-Xlint:deprecation")
}
}
}

View File

@ -18,10 +18,24 @@
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.content.Intent.ACTION_CREATE_SHORTCUT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".OpenUrl"/>
<activity-alias
android:name=".OpenUrl"
android:exported="true"
android:targetActivity=".MainActivity">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="burguillos.info"/>
</intent-filter>
</activity-alias>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

View File

@ -2,6 +2,7 @@ package info.burguillos.bi;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebResourceRequest;
import android.webkit.JavascriptInterface;
import android.util.Log;
@ -11,6 +12,7 @@ import android.content.Intent;
import android.app.PendingIntent;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.view.View;
import android.net.Uri;
import android.widget.RelativeLayout;
import android.content.pm.ShortcutInfo;
import android.graphics.BitmapFactory;
@ -31,15 +33,15 @@ public class MainActivity extends AppCompatActivity {
private RelativeLayout relativeLayout;
private static Bitmap getBitmap(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Bitmap d = BitmapFactory.decodeStream(is);
is.close();
return d;
} catch (Exception e) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Bitmap d = BitmapFactory.decodeStream(is);
is.close();
return d;
} catch (Exception e) {
Log.w("BurguillosInfo", e);
return null;
}
return null;
}
}
@Override
@ -88,15 +90,15 @@ public class MainActivity extends AppCompatActivity {
targetIntent.putExtra("url", url);
Log.w("BurguillosInfo", "Created intent " + url + " " + shortLabel);
Bitmap bitmapIcon = getBitmap(iconUrl);
if (bitmapIcon == null) {
Log.w("BurguillosInfo", "No bitmap.");
}
Bitmap bitmapIcon = getBitmap(iconUrl);
if (bitmapIcon == null) {
Log.w("BurguillosInfo", "No bitmap.");
}
ShortcutInfo pinShortcutInfo =
new ShortcutInfo.Builder(MainActivity.this, url)
.setShortLabel(shortLabel)
.setIntent(targetIntent)
.setIcon(Icon.createWithBitmap(getBitmap(iconUrl)))
.setIcon(Icon.createWithBitmap(getBitmap(iconUrl)))
.build();
Log.w("BurguillosInfo", "Built pin.");
@ -105,17 +107,11 @@ public class MainActivity extends AppCompatActivity {
}
}
webView.addJavascriptInterface(new WebCallsAndroidCode(), "Android");
Bundle resultIntent = getIntent().getExtras();
Intent resultIntent = getIntent();
String url = "https://burguillos.info";
if(resultIntent != null)
{
String paramURL = resultIntent.getString("url");
if (paramURL != null) {
Log.w("BurguillosInfo", paramURL);
url += paramURL;
}
}
String baseUrl = "https://burguillos.info";
String url = baseUrl;
url = getCorrectUrlForIntent(resultIntent, url);
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
@ -126,9 +122,41 @@ public class MainActivity extends AppCompatActivity {
public void onPageStarted(WebView view, String url) {
}
@Override
public boolean shouldOverrideUrlLoading(WebView view,
WebResourceRequest request) {
Uri uri = request.getUrl();
if (!uri.toString().startsWith(baseUrl) && !uri.toString().startsWith("/")) {
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return true;
}
return false;
}
});
}
public static String getCorrectUrlForIntent(Intent resultIntent, String url) {
Bundle extras = resultIntent.getExtras();
if(extras != null)
{
String paramURL = extras.getString("url");
if (paramURL != null) {
return url + paramURL;
}
}
Uri possibleUri = resultIntent.getData();
if (possibleUri== null) {
return url;
}
String possibleUrl = possibleUri.toString();
if (possibleUrl.startsWith("http")) {
return possibleUrl;
}
Log.w("BurguillosInfo", url);
return url;
}
@Override
public void onBackPressed() {
WebView webView = findViewById(R.id.web);

View File

@ -0,0 +1,30 @@
package info.burguillos.bi;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebResourceRequest;
import android.webkit.JavascriptInterface;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.view.MotionEvent;
import android.content.Intent;
import android.app.PendingIntent;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.view.View;
import android.net.Uri;
import android.widget.RelativeLayout;
import android.content.pm.ShortcutInfo;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.graphics.drawable.Icon;
import java.io.InputStream;
import java.net.URL;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.pm.ShortcutManager;
public class OpenUrl extends AppCompatActivity {
}