|
1 | 1 | package com.onedreamers.musicplayer;
|
2 | 2 |
|
3 |
| -import android.Manifest; |
4 |
| -import android.annotation.TargetApi; |
5 |
| -import android.content.Context; |
6 |
| -import android.content.ContextWrapper; |
7 |
| -import android.content.Intent; |
8 |
| -import android.graphics.Bitmap; |
9 |
| -import android.graphics.BitmapFactory; |
10 |
| -import android.media.MediaMetadataRetriever; |
11 |
| -import android.net.Uri; |
12 |
| -import android.os.Build; |
13 | 3 | import android.os.Bundle;
|
14 | 4 | import android.util.Log;
|
15 | 5 | import io.flutter.app.FlutterActivity;
|
16 | 6 | import io.flutter.plugin.common.MethodCall;
|
17 | 7 | import io.flutter.plugin.common.MethodChannel;
|
| 8 | +import io.flutter.plugin.common.MethodChannel.MethodCallHandler; |
18 | 9 | import io.flutter.plugins.GeneratedPluginRegistrant;
|
19 | 10 |
|
20 |
| -import java.io.File; |
21 |
| -import java.io.FileOutputStream; |
22 |
| -import java.io.IOException; |
23 |
| -import java.util.HashMap; |
24 |
| -import java.util.Map; |
25 |
| - |
26 | 11 | public class MainActivity extends FlutterActivity {
|
27 |
| - MediaMetadataRetriever metaRetriver; |
28 |
| - byte[] art; |
29 |
| - Map<String, String> song; |
30 |
| - Uri file; |
31 |
| - Intent intent; |
32 |
| - int requestcode = 1; |
33 |
| - |
34 |
| - @TargetApi(Build.VERSION_CODES.M) |
35 | 12 | @Override
|
36 | 13 | protected void onCreate(Bundle savedInstanceState) {
|
37 | 14 | super.onCreate(savedInstanceState);
|
38 | 15 | GeneratedPluginRegistrant.registerWith(this);
|
39 |
| - try { |
40 |
| - intent = getIntent(); |
41 |
| - String action = intent.getAction(); |
42 |
| - String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; |
43 |
| - if (action == Intent.ACTION_VIEW) { |
44 |
| - requestPermissions(permissions, requestcode); |
45 |
| - handle(intent); |
46 |
| - new MethodChannel(getFlutterView(), "app.channel.shared.data").setMethodCallHandler(new MethodChannel.MethodCallHandler() { |
47 |
| - @Override |
48 |
| - public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { |
49 |
| - if (methodCall.method.contentEquals("getSharedData")) { |
50 |
| - result.success(song); |
51 |
| - } |
52 |
| - } |
53 |
| - }); |
54 |
| - } |
55 |
| - } catch (Exception e) { |
56 |
| - Log.d("cdve", e.getMessage()); |
57 |
| - } |
58 |
| - } |
59 |
| - |
60 |
| - void handle(Intent intent) { |
61 |
| - file = intent.getData(); |
62 |
| - song = new HashMap<>(); |
63 |
| - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) { |
64 |
| - metaRetriver = new MediaMetadataRetriever(); |
65 |
| - metaRetriver.setDataSource(file.getPath()); |
66 |
| - try { |
67 |
| - art = metaRetriver.getEmbeddedPicture(); |
68 |
| - song.put("album", metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)); |
69 |
| - song.put("artist", metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)); |
70 |
| - song.put("duration", metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)); |
71 |
| - String title = metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE); |
72 |
| - song.put("title", title); |
73 |
| - song.put("uri", file.getPath()); |
74 |
| - if (art != null) { |
75 |
| - Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length); |
76 |
| - song.put("albumArt", saveToInternalStorage(songImage, title)); |
77 |
| - } else { |
78 |
| - song.put("albumArt", null); |
| 16 | + new MethodChannel(getFlutterView(), "android_app_retain").setMethodCallHandler(new MethodCallHandler() { |
| 17 | + @Override |
| 18 | + public void onMethodCall(MethodCall call, MethodChannel.Result result) { |
| 19 | + if (call.method.equals("sendToBackground")) { |
| 20 | + Log.d("abc", "Backkkkk"); |
| 21 | + moveTaskToBack(true); |
79 | 22 | }
|
80 |
| - |
81 |
| - } catch (Exception e) { |
82 |
| - Log.d("23456543", e.getMessage()); |
83 | 23 | }
|
84 |
| - } |
85 |
| - } |
| 24 | + }); |
86 | 25 |
|
87 |
| - private String saveToInternalStorage(Bitmap bitmapImage, String title) { |
88 |
| - ContextWrapper cw = new ContextWrapper(getApplicationContext()); |
89 |
| - File directory = cw.getDir("thumbs", Context.MODE_PRIVATE); |
90 |
| - |
91 |
| - File file = new File(directory, title + ".png"); |
92 |
| - FileOutputStream fos = null; |
93 |
| - try { |
94 |
| - fos = new FileOutputStream(file); |
95 |
| - bitmapImage.compress(Bitmap.CompressFormat.PNG, 50, fos); |
96 |
| - } catch (Exception e) { |
97 |
| - e.printStackTrace(); |
98 |
| - } finally { |
99 |
| - try { |
100 |
| - fos.close(); |
101 |
| - } catch (IOException e) { |
102 |
| - e.printStackTrace(); |
103 |
| - } |
104 |
| - } |
105 |
| - return file.getAbsolutePath(); |
106 | 26 | }
|
107 | 27 | }
|
0 commit comments