-
Notifications
You must be signed in to change notification settings - Fork 35
/
playlistHandler.java
183 lines (147 loc) · 6.78 KB
/
playlistHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.player;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import com.player.data.Playlist;
import java.io.File;
import java.util.ArrayList;
public class playlistHandler {
public static ArrayList<String[]> getALlPlalists(ContentResolver Cr){
ArrayList<String[]> list = new ArrayList<String[]>();
Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
String[] cols = new String[] {MediaStore.Audio.Playlists.NAME, MediaStore.Audio.Playlists._ID};
Cursor DataCursor = Cr.query( uri,cols, null,null, MediaStore.Audio.Playlists.NAME +" COLLATE NOCASE ASC");// +" COLLATE NOCASE ASC");
int length = DataCursor.getCount();
for (int i = 0; i < length; i++) {
DataCursor.moveToNext();
String str = DataCursor.getString(0);
if(!str.startsWith(" ") && str.length() != 0){
list.add(new String[] {str,DataCursor.getInt(1)+""});
}
}
DataCursor.close();
return list;
}
public static void addTOPlaylistByID(ContentResolver Cn,int plalistId,int[] AIDs){
String[] cols = new String[] { "count(*)" };
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", plalistId);
Cursor cur = Cn.query(uri, cols, null, null, null);
final int base;
cur.moveToNext();
base = cur.getInt(0);
cur.close();
for(int i = 0;i < AIDs.length;i++){
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID,AIDs[i]);
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf( base+AIDs[i]));
Cn.insert(uri, values);
}
}
public static void addTOPlaylistByID(ContentResolver Cn,int plalistId,Integer[] AIDs){
String[] cols = new String[] { "count(*)" };
Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", plalistId);
Cursor cur = Cn.query(uri, cols, null, null, null);
final int base;
cur.moveToNext();
base = cur.getInt(0);
cur.close();
for(int i = 0;i < AIDs.length;i++){
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID,AIDs[i]);
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf( base+AIDs[i]));
Cn.insert(uri, values);
}
}
public static int getPlaylistIdByName(ContentResolver Cr, String Name) {
int Id = -1;
Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
String[] cols = new String[] { MediaStore.Audio.Playlists._ID};
Cursor DataCursor = Cr.query( uri,cols, MediaStore.Audio.Playlists.NAME + " = '" + Name +"'",null, MediaStore.Audio.Playlists.NAME +" COLLATE NOCASE ASC");
int length = DataCursor.getCount();
for (int i = 0; i < length; i++) {
DataCursor.moveToNext();
Id = DataCursor.getInt(0);
}
DataCursor.close();
return Id;
}
public static String getPlaylistNameById(ContentResolver Cr, int ID) {
String Name = null;
Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
String[] cols = new String[] { MediaStore.Audio.Playlists.NAME};
Cursor DataCursor = Cr.query( uri,cols, MediaStore.Audio.Playlists._ID + " = " + ID ,null, MediaStore.Audio.Playlists.NAME +" COLLATE NOCASE ASC");
int length = DataCursor.getCount();
for (int i = 0; i < length; i++) {
DataCursor.moveToNext();
Name = DataCursor.getString(0);
}
DataCursor.close();
return Name;
}
public static ArrayList getPlaylistByIds(ContentResolver Cr, int Id) {
ArrayList list = null;
String[] projection = {MediaStore.Audio.Playlists.Members.TITLE,MediaStore.Audio.Playlists.Members.AUDIO_ID,MediaStore.Audio.Playlists.Members.DURATION};
Cursor DataCursor = Cr.query(
MediaStore.Audio.Playlists.Members.getContentUri("external", Id), projection,
MediaStore.Audio.Playlists.Members.IS_MUSIC + " != 0 ", null, MediaStore.Audio.Playlists.Members.PLAY_ORDER +" ASC");
int length = DataCursor.getCount();
if(length != 0){
list = new ArrayList<String[]>();
}
for (int i = 0; i < length; i++) {
DataCursor.moveToNext();
list.add(new String[] {DataCursor.getString(0),DataCursor.getString(1),DataCursor.getString(2),""});
}
DataCursor.close();
return list;
}
public static int getPlaylistLength(ContentResolver Cr, int Id) {
ArrayList list = null;
String[] projection = {"count(*)"};
Cursor DataCursor = Cr.query(
MediaStore.Audio.Playlists.Members.getContentUri("external", Id), projection,
MediaStore.Audio.Media.IS_MUSIC + " != 0 ", null, null);
int length = DataCursor.getCount();
if(length > 0){
DataCursor.moveToNext();
length = DataCursor.getInt(0);
}
return length;
}
public static ArrayList getPlaylistByNames(ContentResolver Cr,int Id) {
ArrayList list = null;
String[] projection = {MediaStore.Audio.Playlists.Members.DISPLAY_NAME};
Cursor DataCursor = Cr.query(
MediaStore.Audio.Playlists.Members.getContentUri("external", Id), projection,
MediaStore.Audio.Media.IS_MUSIC + " != 0 ", null, null);
int length = DataCursor.getCount();
if(length != 0){
list = new ArrayList<String>();
}
for (int i = 0; i < length; i++) {
DataCursor.moveToNext();
list.add(DataCursor.getString(0));
}
DataCursor.close();
return list;
}
public static Uri addNewPlaylist(ContentResolver Cr, String str){
Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.NAME, str);
values.put(MediaStore.Audio.Playlists.DATE_ADDED,
System.currentTimeMillis());
values.put(MediaStore.Audio.Playlists.DATE_MODIFIED,
System.currentTimeMillis());
Uri Got = Cr.insert(uri, values);
return Got;
}
public static void updateName(ContentResolver Cr, String str, String name) {
Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Playlists.NAME, str);
int Got = Cr.update(uri, values,MediaStore.Audio.Playlists.NAME + " = ?" ,new String[]{name});
}
}