Adding support for pin to home.

This commit is contained in:
Sergiotarxz 2023-08-29 23:53:46 +02:00
parent f6108d809c
commit f850026304
4 changed files with 104 additions and 28 deletions

View File

@ -8,7 +8,7 @@ android {
defaultConfig {
applicationId = "info.burguillos.bi"
minSdk = 24
minSdk = 26
targetSdk = 33
versionCode = 1
versionName = "1.0"

View File

@ -10,6 +10,7 @@
android:roundIcon="@drawable/burguillos"
android:supportsRtl="true"
android:theme="@style/Theme.BInfoAndroid"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".MainActivity"

View File

@ -2,17 +2,27 @@ package info.burguillos.bi;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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.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 MainActivity extends AppCompatActivity {
@ -20,6 +30,18 @@ public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipeRefreshLayout;
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) {
Log.w("BurguillosInfo", e);
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -28,40 +50,93 @@ public class MainActivity extends AppCompatActivity {
webView = findViewById(R.id.web);
swipeRefreshLayout = findViewById(R.id.swipeContainer);
relativeLayout = findViewById(R.id.relative);
swipeRefreshLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
@Override
public void onRefresh() {
webView.reload();
}
});
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction() & MotionEvent.ACTION_MASK;
if (action != MotionEvent.ACTION_DOWN) {
return false;
}
if (event.getY() > 70) {
swipeRefreshLayout.setEnabled(false);
return false;
}
swipeRefreshLayout.setEnabled(true);
return false;
}
});
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction() & MotionEvent.ACTION_MASK;
if (action != MotionEvent.ACTION_DOWN) {
return false;
}
if (event.getY() > 70) {
swipeRefreshLayout.setEnabled(false);
return false;
}
swipeRefreshLayout.setEnabled(true);
return false;
}
});
webView.loadUrl("https://burguillos.info");
class WebCallsAndroidCode {
@JavascriptInterface
public void pinPage(String url, String shortLabel, String iconUrl) {
ShortcutManager shortcutManager =
MainActivity.this.getSystemService(ShortcutManager.class);
if (!shortcutManager.isRequestPinShortcutSupported()) {
Log.w("BurguillosInfo", "Unable to pin.");
return;
}
Intent targetIntent = new Intent(MainActivity.this.getApplicationContext(), MainActivity.class);
targetIntent.setAction(Intent.ACTION_DEFAULT);
targetIntent.putExtra("url", url);
Log.w("BurguillosInfo", "Created intent " + url + " " + shortLabel);
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)))
.build();
Log.w("BurguillosInfo", "Built pin.");
Intent pinnedShortcutCallbackIntent =
shortcutManager.createShortcutResultIntent(pinShortcutInfo);
Log.w("BurguillosInfo", "created pin.");
PendingIntent successCallback = PendingIntent.getBroadcast(MainActivity.this, 0,
pinnedShortcutCallbackIntent,
PendingIntent.FLAG_IMMUTABLE);
Log.w("BurguillosInfo", "added success pin.");
shortcutManager.requestPinShortcut(pinShortcutInfo,
successCallback.getIntentSender());
Log.w("BurguillosInfo", "added request pin.");
}
}
webView.addJavascriptInterface(new WebCallsAndroidCode(), "Android");
Bundle resultIntent = getIntent().getExtras();
String url = "https://burguillos.info";
if(resultIntent != null)
{
String paramURL = resultIntent.getString("url");
if (paramURL != null) {
Log.w("BurguillosInfo", paramURL);
url += paramURL;
}
}
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
}
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
}
public void onPageStarted(WebView view, String url) {
}
});
public void onPageStarted(WebView view, String url) {
}
});
}
@Override