Skip to content

Commit

Permalink
3586 SlashScreen,get Local music,Remote Music
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhquang252 committed Nov 22, 2018
1 parent 0cf8893 commit ec3fec3
Show file tree
Hide file tree
Showing 19 changed files with 540 additions and 23 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
buildConfigField "String", "API_KEY", "\"${API_KEY}\""
}
buildTypes {
release {
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.framgia.quangtran.music_42">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.framgia.quangtran.music_42.ui.slashscreen.MainActivity">
<activity android:name="com.framgia.quangtran.music_42.ui.slashscreen.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.framgia.quangtran.music_42.ui.homescreen.HomeActivity" />
</application>

</manifest>
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);
}
}
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);
}
}
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();
}
}
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;
}
}
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);
}
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;
}
}
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 app/src/main/java/com/framgia/quangtran/music_42/ui/Contracts.java
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";
}
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);
}
}
Loading

0 comments on commit ec3fec3

Please sign in to comment.