Skip to content
This repository has been archived by the owner on Jul 23, 2020. It is now read-only.

Commit

Permalink
added dynamics shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
raulhaag committed Nov 10, 2019
1 parent 5c615d1 commit 2bcbffb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/src/main/java/ar/rulosoft/mimanganu/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CustomExceptionHandler.Attach(this);
if (getIntent() != null) {
int t;
if (-1 != (t = getIntent().getIntExtra("manga_id", -1))) {
onNewIntent(getIntent());
}
}
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/ar/rulosoft/mimanganu/MainFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ public void onStart() {

@Override
public void onPause() {
super.onPause();
if (swipeReLayout != null)
swipeReLayout.clearAnimation();
super.onPause();

}

@Override
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/ar/rulosoft/mimanganu/MangaFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import ar.rulosoft.mimanganu.componentes.Database;
import ar.rulosoft.mimanganu.componentes.Manga;
import ar.rulosoft.mimanganu.componentes.ReaderOptions;
import ar.rulosoft.mimanganu.componentes.Shortcuts;
import ar.rulosoft.mimanganu.servers.FromFolder;
import ar.rulosoft.mimanganu.servers.ServerBase;
import ar.rulosoft.mimanganu.services.DownloadPoolService;
Expand Down Expand Up @@ -263,6 +264,7 @@ public void onItemCheckedStateChanged(
loadInfo(mManga);
chapters_order = pm.getInt(CHAPTERS_ORDER, 1);
hide_read = pm.getBoolean(CHAPTERS_HIDE_READ, false);
Shortcuts.addShortCuts(mManga, getActivity());
}
}

Expand Down
80 changes: 80 additions & 0 deletions app/src/main/java/ar/rulosoft/mimanganu/componentes/Shortcuts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ar.rulosoft.mimanganu.componentes;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.os.Environment;
import android.preference.PreferenceManager;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import ar.rulosoft.mimanganu.MainActivity;
import ar.rulosoft.mimanganu.R;

public class Shortcuts {
public static void addShortCuts(Manga m, Context ctx) {
if (m.getVault().isEmpty()) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
ShortcutManager sm = ctx.getSystemService(ShortcutManager.class);
List<ShortcutInfo> sl = sm.getDynamicShortcuts();
String icdir = prefs.getString("directorio",
Environment.getExternalStorageDirectory().getAbsolutePath()) + "/MiMangaNu/";
icdir = icdir + "cache/";
Bitmap bm = decodeFile(new File(icdir + m.getImages().hashCode()));
Icon icon = null;

if (bm == null) {
icon = Icon.createWithResource(ctx, R.drawable.noimage);
} else {
icon = Icon.createWithAdaptiveBitmap(bm);
}
Intent i = new Intent(ctx, MainActivity.class);
i.putExtra("manga_id", m.getId());
i.setAction(Intent.ACTION_VIEW);
ShortcutInfo s = new ShortcutInfo.Builder(ctx, m.getTitle() + m.getServerId())
.setShortLabel(m.getTitle())
.setIcon(icon)
.setIntent(i)
.build();
if (!sl.contains(s)) {
sl.add(s);
sm.addDynamicShortcuts(Arrays.asList(s));
}
if (sl.size() > 4) {
sm.removeDynamicShortcuts(Arrays.asList(sl.get(0).getId()));
}
}
}
}

private static Bitmap decodeFile(File put_file) {
// if file not exist, skip everything
if (!put_file.exists())
return null;
// We want Image to be equal or smaller than 200px height
int tempSampleSize = 1, requiredSize = 400;
try {
BitmapFactory.Options bmpOpts = new BitmapFactory.Options();
bmpOpts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(put_file.getAbsolutePath(), bmpOpts);
while ((bmpOpts.outHeight / tempSampleSize) >= requiredSize) {
tempSampleSize *= 2;
}
bmpOpts.inSampleSize = tempSampleSize;
bmpOpts.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(put_file.getAbsolutePath(), bmpOpts);
} catch (Exception e) {
// usually file not found, but just ignore it
return null;
}
}
}

0 comments on commit 2bcbffb

Please sign in to comment.