This repository has been archived by the owner on Jul 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
app/src/main/java/ar/rulosoft/mimanganu/componentes/Shortcuts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |