Created
December 6, 2015 12:19
-
-
Save huseyinbarin/4f8ab7816587bdbb5fe0 to your computer and use it in GitHub Desktop.
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
package barin.com.soundcloudapp.services; | |
import android.app.IntentService; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.support.v4.content.LocalBroadcastManager; | |
import android.util.Log; | |
import org.json.JSONException; | |
import java.lang.ref.WeakReference; | |
import java.util.ArrayList; | |
import java.util.List; | |
import barin.com.soundcloudapp.application.AppParams; | |
import barin.com.soundcloudapp.be.SoundCloudUser; | |
import barin.com.soundcloudapp.be.Track; | |
import barin.com.soundcloudapp.dao.NetworkDAO; | |
import barin.com.soundcloudapp.database.tables.TableSoundCloudUser; | |
import barin.com.soundcloudapp.database.tables.TableTrack; | |
import barin.com.soundcloudapp.managers.SharedPreferencesManager; | |
import barin.com.soundcloudapp.providers.TracksContract; | |
import barin.com.soundcloudapp.utils.Constants; | |
/** | |
* Created by barin.huseyin on 11/9/2015. | |
*/ | |
/** | |
* SoundCloudIntentService is basically background service running periodically to update the app checking soundcloud api | |
* | |
*/ | |
public class SoundCloudIntentService extends IntentService { | |
int counter = 0; | |
private static final String TAG = SoundCloudIntentService.class.getSimpleName(); | |
public static final int STATUS_SUCCESS= 1; | |
public static final int STATUS_ERROR = 0; | |
private SharedPreferences prefs; | |
private SharedPreferences.Editor editor; | |
SoundCloudUser soundCloudUser; | |
List<Track> tracks; | |
Intent localIntent; | |
public SoundCloudIntentService() { | |
super(TAG); | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
prefs = SharedPreferencesManager.getInstance(getBaseContext()).getPref(); | |
editor = prefs.edit(); | |
} | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
soundCloudUser = new SoundCloudUser(); | |
tracks = new ArrayList<Track>(); | |
localIntent = new Intent(Constants.BROADCAST_ACTION); | |
if (AppParams.DEBUG) { | |
Log.d(TAG, "*************************************@onHandleIntent service is ready to handle*******************************************************"); | |
} | |
//Create two parallel threads for network requests | |
RequestUserInfoThread requestUserInfoThread = new RequestUserInfoThread(this); | |
RequestTrackInfo requestTrackInfo = new RequestTrackInfo(this); | |
//start these request as parallel | |
requestUserInfoThread.start(); | |
requestTrackInfo.start(); | |
try { | |
//use join to update the UI in a single moment | |
requestUserInfoThread.join(); | |
requestTrackInfo.join(); | |
if (AppParams.DEBUG) { | |
Log.i(TAG, "@Both of two threads finished their jobs"); | |
} | |
} catch (Exception e) { | |
localIntent.putExtra(Constants.EXTENDED_DATA_STATUS, STATUS_ERROR); | |
localIntent.putExtra(Constants.ERROR_MESSAGE, e.getMessage().toString()); | |
} | |
if (soundCloudUser != null && tracks.size() > 0) { | |
counter++; | |
Log.d(TAG, "" + counter); | |
insertTracksLocally(tracks); | |
insertUserLocally(soundCloudUser); | |
//give a message the ui to be sure the service periodically(2 m) check the api... | |
localIntent.putExtra(Constants.EXTRA_DATA_USER, soundCloudUser.getFirstname()); | |
localIntent.putExtra(Constants.EXTRA_DATA_TRACK, "Tracks sice: " + tracks.size()+" " +tracks.get(0).getTitle()); | |
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent); | |
} | |
} | |
/** | |
* Insert the tracks list to virtual table of tracks through the contentresolver | |
* @param tracks | |
*/ | |
private void insertTracksLocally(List<Track> tracks) { | |
try { | |
ContentValues trackValues; | |
for (Track track : tracks) { | |
trackValues = new ContentValues(); | |
trackValues.put(TableTrack.COL_TRACK_ID, track.getTrackId()); | |
trackValues.put(TableTrack.COL_TITLE, track.getTitle()); | |
trackValues.put(TableTrack.COL_STREAM_URL, track.getStreamUrl()); | |
trackValues.put(TableTrack.COL_ART_NETWORK_URL, track.getArtNetworkUrl()); | |
trackValues.put(TableTrack.COL_DOWNLOADABLE, track.isDownloadable()); | |
trackValues.put(TableTrack.COL_FK, track.getUserId()); | |
trackValues.put(TableTrack.COL_CREATED_AT, track.getCreatedAt()); | |
getContentResolver().insert(TracksContract.URI_TRACKS, trackValues); | |
} | |
} catch (Exception e) { | |
if (AppParams.DEBUG) { | |
Log.e(TAG, e.getMessage().toString()); | |
} | |
localIntent.putExtra(Constants.EXTENDED_DATA_STATUS, STATUS_ERROR); | |
throw e; | |
} | |
} | |
/** | |
* Insert the soundcloud to virtual table of soundclouduser through the contentresolver | |
* @param soundCloudUser | |
*/ | |
private void insertUserLocally(SoundCloudUser soundCloudUser) { | |
if (soundCloudUser.getUserId() != null) { | |
try { | |
ContentValues userValues = new ContentValues(); | |
userValues.put(TableSoundCloudUser.COL_USER_ID, soundCloudUser.getUserId()); | |
userValues.put(TableSoundCloudUser.COL_USERNAME, soundCloudUser.getUsername()); | |
userValues.put(TableSoundCloudUser.COL_LASTNAME, soundCloudUser.getLastname()); | |
userValues.put(TableSoundCloudUser.COL_AVATAR_URL, soundCloudUser.getAvatarUrl()); | |
userValues.put(TableSoundCloudUser.COL_DESCRIPTION, soundCloudUser.getDescription()); | |
userValues.put(TableSoundCloudUser.COL_CITY, soundCloudUser.getCity()); | |
userValues.put(TableSoundCloudUser.COL_ONLINE, soundCloudUser.isOnline()); | |
userValues.put(TableSoundCloudUser.COL_TRACK_COUNT, soundCloudUser.getTrackCount()); | |
userValues.put(TableSoundCloudUser.COL_PLAYLIST_COUNT, soundCloudUser.getPlaylistCount()); | |
userValues.put(TableSoundCloudUser.COL_FOLLOWER_COUNT, soundCloudUser.getFollowerCount()); | |
userValues.put(TableSoundCloudUser.COL_FOLLOWING_COUNT, soundCloudUser.getFollowingCount()); | |
userValues.put(TableSoundCloudUser.COL_LAST_MODIFIED_TIME, soundCloudUser.getLast_modified()); | |
getContentResolver().insert(TracksContract.URI_USERS, userValues); | |
} catch (Exception e) { | |
if (AppParams.DEBUG) { | |
Log.e(TAG, e.getMessage().toString()); | |
} | |
localIntent.putExtra(Constants.EXTENDED_DATA_STATUS, STATUS_ERROR); | |
throw e; | |
} | |
} | |
} | |
/** | |
* Simple Thread for the requesting soundcloud user info | |
*/ | |
private class RequestUserInfoThread extends Thread { | |
NetworkDAO mNettworkDAO; | |
Context mContext; | |
WeakReference<Context> weakReference; | |
public RequestUserInfoThread(Context context) { | |
// weakReference = new WeakReference<Context>(context); | |
//mContext = weakReference.get(); | |
mContext = context; | |
mNettworkDAO = new NetworkDAO(mContext); | |
} | |
Exception ex = null; | |
@Override | |
public void run() { | |
try { | |
synchronized (soundCloudUser) { | |
soundCloudUser = (mNettworkDAO.getUserInfo("3207")); | |
} | |
//insert the lcoal database the results here. | |
} catch (JSONException e) { | |
if (AppParams.DEBUG) { | |
Log.e(TAG, e.getMessage().toString()); | |
} | |
ex = e; | |
} catch (Exception e1) { | |
ex = e1; | |
} | |
if (ex != null) { | |
localIntent.putExtra(Constants.EXTENDED_DATA_STATUS, STATUS_ERROR); | |
localIntent.putExtra(Constants.ERROR_MESSAGE, ex.getMessage().toString()); | |
} else { | |
localIntent.putExtra(Constants.EXTENDED_DATA_STATUS, STATUS_SUCCESS); | |
} | |
} | |
} | |
/** | |
* Simple Thread for the requesting track info user info from the api. | |
*/ | |
private class RequestTrackInfo extends Thread { | |
Exception ex = null; | |
NetworkDAO mNettworkDAO; | |
Context mContext; | |
WeakReference<Context> weakReference; | |
public RequestTrackInfo(Context context) { | |
// weakReference = new WeakReference<Context>(context); | |
// mContext = weakReference.get(); | |
mContext = context; | |
mNettworkDAO = new NetworkDAO(mContext); | |
} | |
@Override | |
public void run() { | |
try { | |
synchronized (tracks) { | |
tracks = mNettworkDAO.getTracksInfo("3207"); | |
} | |
if (AppParams.DEBUG) { | |
Log.i(TAG, "Service Side Tracks size:" + tracks.size()); | |
} | |
//insert tracks the lcoal database the results here. | |
} catch (JSONException e) { | |
if (AppParams.DEBUG) { | |
Log.e(TAG, e.getMessage().toString()); | |
} | |
ex = e; | |
} catch (Exception e1) { | |
ex = e1; | |
} | |
if (ex != null) { | |
localIntent.putExtra(Constants.EXTENDED_DATA_STATUS, STATUS_ERROR); | |
localIntent.putExtra(Constants.ERROR_MESSAGE, ex.getMessage().toString()); | |
} else { | |
localIntent.putExtra(Constants.EXTENDED_DATA_STATUS, STATUS_SUCCESS); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment