-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3586 SlashScreen,get Local music,Remote Music
- Loading branch information
1 parent
0cf8893
commit ec3fec3
Showing
19 changed files
with
540 additions
and
23 deletions.
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
30 changes: 29 additions & 1 deletion
30
app/src/main/java/com/framgia/quangtran/music_42/data/repository/TrackRepository.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 |
---|---|---|
@@ -1,10 +1,38 @@ | ||
package com.framgia.quangtran.music_42.data.repository; | ||
|
||
import android.content.ContentResolver; | ||
|
||
import com.framgia.quangtran.music_42.data.model.Track; | ||
import com.framgia.quangtran.music_42.data.source.TrackDataSource; | ||
|
||
public class TrackRepository implements TrackDataSource.Local, TrackDataSource.Remote { | ||
private static TrackRepository sInstance; | ||
private TrackDataSource.Remote mRemoteDataSource; | ||
private TrackDataSource.Local mLocalDataSource; | ||
|
||
private TrackRepository(TrackDataSource.Remote remoteDataSource, | ||
TrackDataSource.Local localDataSource) { | ||
mRemoteDataSource = remoteDataSource; | ||
mLocalDataSource = localDataSource; | ||
} | ||
|
||
public static synchronized TrackRepository | ||
getInstance(TrackDataSource.Remote remoteDataSource, | ||
TrackDataSource.Local localDataSource) { | ||
if (sInstance == null) { | ||
sInstance = new TrackRepository(remoteDataSource, localDataSource); | ||
} | ||
return sInstance; | ||
} | ||
|
||
@Override | ||
public void getOnlineTrack(String api, TrackDataSource.DataCallback<Track> callback) { | ||
mRemoteDataSource.getOnlineTrack(api, callback); | ||
} | ||
|
||
@Override | ||
public void getOfflineTracks(TrackDataSource.DataCallback<Track> callback) { | ||
public void getOfflineTracks(ContentResolver contentResolver, | ||
TrackDataSource.DataCallback<Track> callback) { | ||
mLocalDataSource.getOfflineTracks(contentResolver, callback); | ||
} | ||
} |
9 changes: 7 additions & 2 deletions
9
app/src/main/java/com/framgia/quangtran/music_42/data/source/TrackDataSource.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 |
---|---|---|
@@ -1,18 +1,23 @@ | ||
package com.framgia.quangtran.music_42.data.source; | ||
|
||
import android.content.ContentResolver; | ||
|
||
import com.framgia.quangtran.music_42.data.model.Track; | ||
|
||
import java.util.List; | ||
|
||
public interface TrackDataSource { | ||
interface DataCallback<T> { | ||
void onSuccess(T data); | ||
void onSuccess(List<T> data); | ||
|
||
void onFailed(String message); | ||
} | ||
|
||
interface Local { | ||
void getOfflineTracks(DataCallback<Track> callback); | ||
void getOfflineTracks(ContentResolver contentResolver, DataCallback<Track> callback); | ||
} | ||
|
||
interface Remote { | ||
void getOnlineTrack(String api, DataCallback<Track> callback); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/framgia/quangtran/music_42/data/source/local/TrackLocalDataSource.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,27 @@ | ||
package com.framgia.quangtran.music_42.data.source.local; | ||
|
||
import android.content.ContentResolver; | ||
import android.util.Log; | ||
|
||
import com.framgia.quangtran.music_42.data.model.Track; | ||
import com.framgia.quangtran.music_42.data.source.TrackDataSource; | ||
|
||
public class TrackLocalDataSource implements TrackDataSource.Local { | ||
private static TrackLocalDataSource sInstance; | ||
|
||
private TrackLocalDataSource() { | ||
} | ||
|
||
public static TrackLocalDataSource getInstance() { | ||
if (sInstance == null) { | ||
sInstance = new TrackLocalDataSource(); | ||
} | ||
return sInstance; | ||
} | ||
|
||
@Override | ||
public void getOfflineTracks(ContentResolver contentResolver, | ||
TrackDataSource.DataCallback<Track> callback) { | ||
new TrackStorageAsyncTask(contentResolver, callback).execute(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...src/main/java/com/framgia/quangtran/music_42/data/source/local/TrackStorageAsyncTask.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,94 @@ | ||
package com.framgia.quangtran.music_42.data.source.local; | ||
|
||
import android.content.ContentResolver; | ||
import android.database.Cursor; | ||
import android.os.AsyncTask; | ||
import android.provider.MediaStore; | ||
|
||
import com.framgia.quangtran.music_42.data.model.Track; | ||
import com.framgia.quangtran.music_42.data.source.TrackDataSource; | ||
import com.framgia.quangtran.music_42.util.StringUtil; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TrackStorageAsyncTask extends AsyncTask<Void, Void, List<Track>> { | ||
private static final String SQL_SELECTION = "=?"; | ||
private static final String MESSAGE = "GET DATA LOCAL IS FAILURE"; | ||
private TrackDataSource.DataCallback<Track> mCallback; | ||
private ContentResolver mContentResolver; | ||
|
||
public TrackStorageAsyncTask(ContentResolver contentResolver, | ||
TrackDataSource.DataCallback<Track> callback) { | ||
this.mContentResolver = contentResolver; | ||
this.mCallback = callback; | ||
} | ||
|
||
@Override | ||
protected List<Track> doInBackground(Void... voids) { | ||
return loadTracks(); | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(List<Track> trackList) { | ||
super.onPostExecute(trackList); | ||
if (trackList != null) { | ||
mCallback.onSuccess(trackList); | ||
} else { | ||
mCallback.onFailed(MESSAGE); | ||
} | ||
} | ||
|
||
private List<Track> loadTracks() { | ||
List<Track> tracks = new ArrayList<>(); | ||
String[] projections = {MediaStore.Audio.Media._ID, | ||
MediaStore.Audio.Media.DISPLAY_NAME, | ||
MediaStore.Audio.Media.ARTIST, | ||
MediaStore.Audio.Media.DATA, | ||
MediaStore.Audio.Media.ALBUM_ID}; | ||
Cursor cursor = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, | ||
projections, null, null, | ||
MediaStore.Audio.Media.DISPLAY_NAME); | ||
if (cursor != null) { | ||
while (cursor.moveToNext()) { | ||
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media | ||
._ID)); | ||
int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media | ||
.ALBUM_ID)); | ||
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media | ||
.DISPLAY_NAME)); | ||
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media | ||
.ARTIST)); | ||
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media | ||
.DATA)); | ||
Track track = new Track(); | ||
track.setDownLoadUrl(path); | ||
track.setArtWorkUrl(getAlbumArt(albumId)); | ||
track.setOffline(true); | ||
tracks.add(track); | ||
} | ||
} | ||
cursor.close(); | ||
return tracks; | ||
} | ||
|
||
private String getAlbumArt(int albumId) { | ||
String[] projections = {MediaStore.Audio.Albums._ID, | ||
MediaStore.Audio.Albums.ALBUM_ART}; | ||
String selection = StringUtil.append(MediaStore.Audio.Albums._ID, | ||
SQL_SELECTION); | ||
String[] selectionArgs = {String.valueOf(albumId)}; | ||
Cursor cursorAlbum = mContentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, | ||
projections, selection, | ||
selectionArgs, | ||
null); | ||
if (cursorAlbum == null) return null; | ||
String artwork = null; | ||
if (cursorAlbum.moveToFirst()) { | ||
artwork = cursorAlbum.getString( | ||
cursorAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)); | ||
} | ||
cursorAlbum.close(); | ||
return artwork; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
.../main/java/com/framgia/quangtran/music_42/data/source/remote/BaseLoadTracksAsyncTask.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,70 @@ | ||
package com.framgia.quangtran.music_42.data.source.remote; | ||
|
||
import android.os.AsyncTask; | ||
|
||
import com.framgia.quangtran.music_42.data.source.TrackDataSource; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.List; | ||
|
||
public abstract class BaseLoadTracksAsyncTask<T> extends AsyncTask<String, String, List<T>> { | ||
private static final String REQUEST_METHOD = "GET"; | ||
private static final int CONNECT_TIMEOUT = 15000; | ||
private static final int READ_TIMEOUT = 15000; | ||
protected TrackDataSource.DataCallback<T> mCallback; | ||
protected Exception mException; | ||
private HttpURLConnection mUrlConnection; | ||
|
||
public BaseLoadTracksAsyncTask(TrackDataSource.DataCallback<T> callback) { | ||
mCallback = callback; | ||
} | ||
|
||
@Override | ||
protected List<T> doInBackground(String... strings) { | ||
String respond = ""; | ||
try { | ||
URL url = new URL(strings[0]); | ||
mUrlConnection = (HttpURLConnection) url.openConnection(); | ||
mUrlConnection.setRequestMethod(REQUEST_METHOD); | ||
mUrlConnection.setConnectTimeout(CONNECT_TIMEOUT); | ||
mUrlConnection.setReadTimeout(READ_TIMEOUT); | ||
mUrlConnection.connect(); | ||
InputStream inputStream = mUrlConnection.getInputStream(); | ||
respond = readResponse(inputStream); | ||
} catch (Exception e) { | ||
mException = e; | ||
} | ||
mUrlConnection.disconnect(); | ||
return convertJson(respond); | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(List<T> list) { | ||
super.onPostExecute(list); | ||
if (mException == null) { | ||
mCallback.onSuccess(list); | ||
} else { | ||
mCallback.onFailed(mException.getMessage()); | ||
} | ||
} | ||
|
||
private String readResponse(InputStream inputStream) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); | ||
StringBuilder sb = new StringBuilder(); | ||
String line; | ||
if (inputStream != null) { | ||
while ((line = br.readLine()) != null) { | ||
sb.append(line); | ||
} | ||
br.close(); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
public abstract List<T> convertJson(String jsonString); | ||
} |
44 changes: 44 additions & 0 deletions
44
app/src/main/java/com/framgia/quangtran/music_42/data/source/remote/LoadTrackAsyncTask.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,44 @@ | ||
package com.framgia.quangtran.music_42.data.source.remote; | ||
|
||
import com.framgia.quangtran.music_42.data.model.Track; | ||
import com.framgia.quangtran.music_42.data.source.TrackDataSource; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LoadTrackAsyncTask extends BaseLoadTracksAsyncTask<Track> { | ||
public static final String ID = "id"; | ||
private static final String TITLE = "title"; | ||
private static final String TRACK = "track"; | ||
private static final String COLLECTION = "collection"; | ||
|
||
public LoadTrackAsyncTask(TrackDataSource.DataCallback<Track> callback) { | ||
super(callback); | ||
} | ||
|
||
@Override | ||
public List<Track> convertJson(String jsonString) { | ||
List<Track> tracks = new ArrayList<>(); | ||
try { | ||
JSONObject result = new JSONObject(jsonString); | ||
JSONArray trackArray = result.getJSONArray(COLLECTION); | ||
for (int i = 0; i < trackArray.length(); i++) { | ||
JSONObject trackDetail = trackArray.getJSONObject(i); | ||
JSONObject trackInfo = trackDetail.getJSONObject(TRACK); | ||
int id = trackInfo.getInt(ID); | ||
String tittle = trackInfo.getString(TITLE); | ||
Track track = new Track(); | ||
track.setId(id); | ||
track.setTitle(tittle); | ||
tracks.add(track); | ||
} | ||
|
||
} catch (Exception e) { | ||
mException = e; | ||
} | ||
return tracks; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...rc/main/java/com/framgia/quangtran/music_42/data/source/remote/TrackRemoteDataSource.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,23 @@ | ||
package com.framgia.quangtran.music_42.data.source.remote; | ||
|
||
import com.framgia.quangtran.music_42.data.model.Track; | ||
import com.framgia.quangtran.music_42.data.source.TrackDataSource; | ||
|
||
public class TrackRemoteDataSource implements TrackDataSource.Remote { | ||
private static TrackRemoteDataSource sInstance; | ||
|
||
private TrackRemoteDataSource() { | ||
} | ||
|
||
public static TrackRemoteDataSource getInstance() { | ||
if (sInstance == null) { | ||
sInstance = new TrackRemoteDataSource(); | ||
} | ||
return sInstance; | ||
} | ||
|
||
@Override | ||
public void getOnlineTrack(String api, TrackDataSource.DataCallback<Track> callback) { | ||
new LoadTrackAsyncTask(callback).execute(api); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/framgia/quangtran/music_42/ui/Contracts.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,23 @@ | ||
package com.framgia.quangtran.music_42.ui; | ||
|
||
public class Contracts { | ||
public static final String CLIENT_ID = "&client_id="; | ||
public static final String BASE_URL_GENRES = "https://api-v2.soundcloud.com/charts?kind=top&genre="; | ||
public static final String BASE_URL_TRACK = "http://api.soundcloud.com/tracks"; | ||
public static final String PARAMETER_LIMIT = "&limit="; | ||
public static final String PARAMETER_OFFSET = "&offset="; | ||
public static final String PARAMETER_SEARCH = "&q="; | ||
public static final String PARAMETER_ID = "?client_id="; | ||
public static final String NAME_STREAM = "stream"; | ||
public static final String SPLASH = "/"; | ||
public static final String QUESTION_MARK = "?"; | ||
public static final int INDEX_UNIT = 1; | ||
public static final int NEXT_SONG = 1; | ||
public static final int PREVIOUS_SONG = -1; | ||
public static final int LIMIT = 10; | ||
public static final String EXTRA_TRACK = "track"; | ||
public static final String EXTRA_GENRES = "genres"; | ||
public static final String EXTRA_TITLE = "title"; | ||
public static final String EXTRA_ERROR = "error"; | ||
public static final String EXTRA_PROGRESS = "progress"; | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/framgia/quangtran/music_42/ui/homescreen/HomeActivity.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,12 @@ | ||
package com.framgia.quangtran.music_42.ui.homescreen; | ||
|
||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v7.app.AppCompatActivity; | ||
|
||
public class HomeActivity extends AppCompatActivity { | ||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
} | ||
} |
Oops, something went wrong.