Added Share instagram stories
This commit is contained in:
parent
5d640b59d1
commit
8e387264d5
16 changed files with 320 additions and 228 deletions
|
@ -1,101 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Hemanth Savarala.
|
||||
*
|
||||
* Licensed under the GNU General Public License v3
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
package code.name.monkey.retromusic.util
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.os.Environment
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
|
||||
/**
|
||||
* Created by hemanths on 2019-11-05.
|
||||
*/
|
||||
|
||||
class ImageSaver(val context: Context) {
|
||||
private var external: Boolean = false
|
||||
private var directoryName: String = "RetroMusic"
|
||||
private var fileName: String = "profile.png"
|
||||
|
||||
fun setFileName(fileName: String): ImageSaver {
|
||||
this.fileName = fileName
|
||||
return this
|
||||
}
|
||||
|
||||
fun setDirectoryName(directoryName: String): ImageSaver {
|
||||
this.directoryName = directoryName
|
||||
return this
|
||||
}
|
||||
|
||||
fun setStoreType(external: Boolean): ImageSaver {
|
||||
this.external = external
|
||||
return this
|
||||
}
|
||||
|
||||
fun save(bitmap: Bitmap) {
|
||||
var fileOutputStream: FileOutputStream? = null
|
||||
try {
|
||||
fileOutputStream = FileOutputStream(createFile())
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)
|
||||
} catch (er: Exception) {
|
||||
println(er)
|
||||
} finally {
|
||||
try {
|
||||
fileOutputStream?.close()
|
||||
} catch (er: IOException) {
|
||||
println(er)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getFile(): File {
|
||||
return createFile()
|
||||
}
|
||||
|
||||
private fun createFile(): File {
|
||||
val directory: File = if (external) {
|
||||
getFileStorePlace(directoryName)
|
||||
} else {
|
||||
context.getDir(directoryName, Context.MODE_PRIVATE)
|
||||
}
|
||||
if (!directory.exists() && !directory.mkdirs()) {
|
||||
println("Error in creating folders $directory")
|
||||
}
|
||||
println("Create file -> $directory/$fileName")
|
||||
return File(directory, fileName)
|
||||
}
|
||||
|
||||
private fun getFileStorePlace(directoryName: String): File {
|
||||
return File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), directoryName)
|
||||
}
|
||||
|
||||
fun load(): Bitmap? {
|
||||
var inputStream: FileInputStream? = null
|
||||
return try {
|
||||
inputStream = FileInputStream(createFile())
|
||||
BitmapFactory.decodeStream(inputStream)
|
||||
} catch (er: Exception) {
|
||||
try {
|
||||
inputStream?.close()
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Hemanth Savarala.
|
||||
*
|
||||
* Licensed under the GNU General Public License v3
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
package code.name.monkey.retromusic.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import code.name.monkey.retromusic.rest.model.LastFmAlbum.Album.Image;
|
||||
import code.name.monkey.retromusic.rest.model.LastFmArtist;
|
||||
|
||||
public class LastFMUtil {
|
||||
|
||||
public static String getLargestAlbumImageUrl(List<Image> list) {
|
||||
Map hashMap = new HashMap();
|
||||
for (Image image : list) {
|
||||
Object obj = null;
|
||||
String size = image.getSize();
|
||||
if (size == null) {
|
||||
obj = ImageSize.UNKNOWN;
|
||||
} else {
|
||||
try {
|
||||
obj = ImageSize.valueOf(size.toUpperCase(Locale.ENGLISH));
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
}
|
||||
if (obj != null) {
|
||||
hashMap.put(obj, image.getText());
|
||||
}
|
||||
}
|
||||
return getLargestImageUrl(hashMap);
|
||||
}
|
||||
|
||||
public static String getLargestArtistImageUrl(List<LastFmArtist.Artist.Image> list) {
|
||||
Map hashMap = new HashMap();
|
||||
for (LastFmArtist.Artist.Image image : list) {
|
||||
Object obj = null;
|
||||
String size = image.getSize();
|
||||
if (size == null) {
|
||||
obj = ImageSize.UNKNOWN;
|
||||
} else {
|
||||
try {
|
||||
obj = ImageSize.valueOf(size.toUpperCase(Locale.ENGLISH));
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
}
|
||||
if (obj != null) {
|
||||
hashMap.put(obj, image.getText());
|
||||
}
|
||||
}
|
||||
return getLargestImageUrl(hashMap);
|
||||
}
|
||||
|
||||
private static String getLargestImageUrl(Map<ImageSize, String> map) {
|
||||
return map.containsKey(ImageSize.MEGA) ? map.get(ImageSize.MEGA)
|
||||
: map.containsKey(ImageSize.EXTRALARGE) ? map.get(ImageSize.EXTRALARGE)
|
||||
: map.containsKey(ImageSize.LARGE) ? map.get(ImageSize.LARGE)
|
||||
: map.containsKey(ImageSize.MEDIUM) ? map.get(ImageSize.MEDIUM)
|
||||
: map.containsKey(ImageSize.SMALL) ? map.get(ImageSize.SMALL)
|
||||
: map.containsKey(ImageSize.UNKNOWN) ? map.get(ImageSize.UNKNOWN) : null;
|
||||
}
|
||||
|
||||
private enum ImageSize {
|
||||
SMALL,
|
||||
MEDIUM,
|
||||
LARGE,
|
||||
EXTRALARGE,
|
||||
MEGA,
|
||||
UNKNOWN
|
||||
}
|
||||
}
|
51
app/src/main/java/code/name/monkey/retromusic/util/Share.kt
Normal file
51
app/src/main/java/code/name/monkey/retromusic/util/Share.kt
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2020 Hemanth Savarala.
|
||||
*
|
||||
* Licensed under the GNU General Public License v3
|
||||
*
|
||||
* This is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
package code.name.monkey.retromusic.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.core.app.ActivityCompat
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
|
||||
/**
|
||||
* Created by hemanths on 2020-02-02.
|
||||
*/
|
||||
|
||||
object Share {
|
||||
|
||||
private const val INSTAGRAM_PACKAGE_NAME = "com.instagram.android"
|
||||
|
||||
fun shareFileToInstagram(context: Context, song: Song, uri: Uri) {
|
||||
val feedIntent = Intent(Intent.ACTION_SEND)
|
||||
feedIntent.type = "image/*"
|
||||
feedIntent.putExtra(Intent.EXTRA_TITLE, song.title)
|
||||
feedIntent.putExtra(Intent.EXTRA_TEXT, song.artistName)
|
||||
feedIntent.putExtra(Intent.EXTRA_STREAM, uri)
|
||||
feedIntent.setPackage(INSTAGRAM_PACKAGE_NAME)
|
||||
|
||||
val storiesIntent = Intent("com.instagram.share.ADD_TO_STORY")
|
||||
storiesIntent.setDataAndType(uri, "jpg")
|
||||
storiesIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
storiesIntent.setPackage(INSTAGRAM_PACKAGE_NAME)
|
||||
storiesIntent.putExtra(Intent.EXTRA_TITLE, song.title)
|
||||
storiesIntent.putExtra(Intent.EXTRA_TEXT, song.artistName)
|
||||
context.grantUriPermission("com.instagram.android", uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
val chooserIntent = Intent.createChooser(feedIntent, context.getString(R.string.social_instagram))
|
||||
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(storiesIntent))
|
||||
ActivityCompat.startActivity(context, chooserIntent, null)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue