Android Open Source - muckebox-android Refresh Helper






From Project

Back to project page muckebox-android.

License

The source code is released under:

Apache License

If you think the Android project muckebox-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*   
 * Copyright 2013 Karsten Patzwaldt//from  w  w w.  j  av a  2 s  .co  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.muckebox.android.net;

import java.io.IOException;
import java.util.ArrayList;

import javax.net.ssl.SSLException;

import org.apache.http.auth.AuthenticationException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.muckebox.android.Muckebox;
import org.muckebox.android.R;
import org.muckebox.android.db.MuckeboxProvider;
import org.muckebox.android.db.MuckeboxContract.AlbumEntry;
import org.muckebox.android.db.MuckeboxContract.ArtistEntry;
import org.muckebox.android.db.MuckeboxContract.TrackEntry;

import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.net.Uri;
import android.os.RemoteException;
import android.util.Log;

public class RefreshHelper {
    private final static String LOG_TAG = "RefreshHelper";
    
    public static Integer refreshTracks(long albumId) {
        try {
            ArrayList<ContentProviderOperation> operations =
                    new ArrayList<ContentProviderOperation>(1);

            JSONArray json = ApiHelper.callApiForArray("tracks", null,
                    new String[] { "album" },
                    new String[] { Long.toString(albumId) });
            operations.ensureCapacity(operations.size() + json.length() + 1);
            
            operations.add(ContentProviderOperation.newDelete(
                    Uri.withAppendedPath(MuckeboxProvider.URI_TRACKS_ALBUM,
                        Long.toString(albumId))).build());

            for (int j = 0; j < json.length(); ++j) {
                JSONObject o = json.getJSONObject(j);
                ContentValues values = new ContentValues();
                
                values.put(TrackEntry.SHORT_ID, o.getInt("id"));
                values.put(TrackEntry.SHORT_ARTIST_ID,  o.getInt("artist_id"));
                values.put(TrackEntry.SHORT_ALBUM_ID, o.getInt("album_id"));
                
                values.put(TrackEntry.SHORT_TITLE, o.getString("title"));
                
                if (! o.isNull("tracknumber"))
                    values.put(TrackEntry.SHORT_TRACKNUMBER, o.getInt("tracknumber"));
                
                if (! o.isNull("discnumber"))
                    values.put(TrackEntry.SHORT_DISCNUMBER, o.getInt("discnumber"));
                
                values.put(TrackEntry.SHORT_LABEL, o.getString("label"));
                values.put(TrackEntry.SHORT_CATALOGNUMBER, o.getString("catalognumber"));
                
                values.put(TrackEntry.SHORT_LENGTH, o.getInt("length"));
                values.put(TrackEntry.SHORT_DATE, o.getString("date"));
                
                operations.add(ContentProviderOperation.newDelete(
                    Uri.withAppendedPath(MuckeboxProvider.URI_TRACKS,
                        Integer.toString(o.getInt("id")))).build());
                operations.add(ContentProviderOperation.newInsert(MuckeboxProvider.URI_TRACKS).
                        withValues(values).build());
            }

            Log.d(LOG_TAG, "Got " + json.length() + " Tracks");
            
            Muckebox.getAppContext().getContentResolver().applyBatch(
                    MuckeboxProvider.AUTHORITY, operations);
        } catch (AuthenticationException e) {
            return R.string.error_authentication;
        } catch (SSLException e) {
            return R.string.error_ssl;
        } catch (IOException e) {
            Log.d(LOG_TAG, "IOException: " + e.getMessage());
            return R.string.error_reload_tracks;
        } catch (JSONException e) {
            return R.string.error_json;
        } catch (RemoteException e) {
            e.printStackTrace();
            return R.string.error_reload_tracks;
        } catch (OperationApplicationException e) {
            e.printStackTrace();
            return R.string.error_reload_tracks;
        } 
        
        return null;
    }
    
    public static Integer refreshAlbums() {
        try {
            JSONArray json = ApiHelper.callApiForArray("albums");
            ArrayList<ContentProviderOperation> operations = 
                    new ArrayList<ContentProviderOperation>(json.length() + 1);
            
            operations.add(
                    ContentProviderOperation.newDelete(MuckeboxProvider.URI_ALBUMS).build());
            
            for (int i = 0; i < json.length(); ++i) {
                JSONObject o = json.getJSONObject(i);

                operations.add(
                        ContentProviderOperation.newInsert(MuckeboxProvider.URI_ALBUMS).
                            withValue(AlbumEntry.SHORT_ID, o.getInt("id")).
                            withValue(AlbumEntry.SHORT_TITLE, o.getString("title")).
                            withValue(AlbumEntry.SHORT_ARTIST_ID, o.getInt("artist_id")).
                            withValue(AlbumEntry.SHORT_CREATED, o.getInt("created")).build());
            }
            
            Muckebox.getAppContext().getContentResolver().applyBatch(
                    MuckeboxProvider.AUTHORITY, operations);
    
            Log.d(LOG_TAG, "Got " + json.length() + " albums");
        } catch (AuthenticationException e) {
            return R.string.error_authentication;
        } catch (SSLException e) {
            return R.string.error_ssl;
        } catch (IOException e) {
            Log.d(LOG_TAG, "IOException: " + e.getMessage());
            return R.string.error_reload_albums;
        } catch (JSONException e) {
            return R.string.error_json;
        } catch (RemoteException e) {
            e.printStackTrace();
            return R.string.error_reload_albums;
        } catch (OperationApplicationException e) {
            e.printStackTrace();
            return R.string.error_reload_albums;
        }
        
        return null;
    }
    
    public static Integer refreshArtists() {
        try {
            JSONArray json = ApiHelper.callApiForArray("artists");
            ArrayList<ContentProviderOperation> operations =
                    new ArrayList<ContentProviderOperation>(json.length() + 1);
            
            operations.add(ContentProviderOperation.newDelete(MuckeboxProvider.URI_ARTISTS).build());
            
            for (int i = 0; i < json.length(); ++i) {
                JSONObject o = json.getJSONObject(i);
                operations.add(ContentProviderOperation.newInsert(MuckeboxProvider.URI_ARTISTS).
                        withValue(ArtistEntry.SHORT_ID, o.getInt("id")).
                        withValue(ArtistEntry.SHORT_NAME, o.getString("name")).build());
            }
            
            Muckebox.getAppContext().getContentResolver().applyBatch(
                    MuckeboxProvider.AUTHORITY, operations);
        } catch (AuthenticationException e) {
            return R.string.error_authentication;
        } catch (SSLException e) {
            return R.string.error_ssl;
        } catch (IOException e) {
            Log.d(LOG_TAG, "IOException: " + e.getMessage());
            return R.string.error_reload_artists;
        } catch (JSONException e) {
            return R.string.error_json;
        } catch (RemoteException e) {
            e.printStackTrace();
            return R.string.error_reload_artists;
        } catch (OperationApplicationException e) {
            e.printStackTrace();
            return R.string.error_reload_artists;
        }
        
        return null;
    }
}




Java Source Code List

org.muckebox.android.Muckebox.java
org.muckebox.android.audio.PlayerListener.java
org.muckebox.android.audio.PlayerService.java
org.muckebox.android.audio.PlayerWrapper.java
org.muckebox.android.db.DownloadEntryCursor.java
org.muckebox.android.db.MuckeboxContract.java
org.muckebox.android.db.MuckeboxDbHelper.java
org.muckebox.android.db.MuckeboxProvider.java
org.muckebox.android.db.PlaylistHelper.java
org.muckebox.android.net.ApiHelper.java
org.muckebox.android.net.DownloadCatchupRunnable.java
org.muckebox.android.net.DownloadListener.java
org.muckebox.android.net.DownloadRunnable.java
org.muckebox.android.net.DownloadServer.java
org.muckebox.android.net.DownloadService.java
org.muckebox.android.net.MuckeboxHttpClient.java
org.muckebox.android.net.PreannounceTask.java
org.muckebox.android.net.RefreshAlbumsTask.java
org.muckebox.android.net.RefreshArtistsAlbumsTask.java
org.muckebox.android.net.RefreshArtistsTask.java
org.muckebox.android.net.RefreshHelper.java
org.muckebox.android.net.RefreshTask.java
org.muckebox.android.net.RefreshTracksTask.java
org.muckebox.android.ui.activity.MuckeboxActivity.java
org.muckebox.android.ui.activity.WizardActivity.java
org.muckebox.android.ui.fragment.AlbumListFragment.java
org.muckebox.android.ui.fragment.ArtistListFragment.java
org.muckebox.android.ui.fragment.DownloadListFragment.java
org.muckebox.android.ui.fragment.DrawerFragment.java
org.muckebox.android.ui.fragment.PlayerFragment.java
org.muckebox.android.ui.fragment.SettingsFragment.java
org.muckebox.android.ui.fragment.TrackListFragment.java
org.muckebox.android.ui.utils.ExpandableCursorAdapter.java
org.muckebox.android.ui.utils.HeightEvaluator.java
org.muckebox.android.ui.utils.ImageButtonHelper.java
org.muckebox.android.ui.utils.NavigationListener.java
org.muckebox.android.ui.utils.ScrollYAnimator.java
org.muckebox.android.ui.utils.TimeFormatter.java
org.muckebox.android.ui.widgets.ImageViewRotater.java
org.muckebox.android.ui.widgets.LiveSearchView.java
org.muckebox.android.ui.widgets.RefreshableListFragment.java
org.muckebox.android.ui.widgets.SearchableListFragment.java
org.muckebox.android.utils.BufferUtils.java
org.muckebox.android.utils.CacheCleaner.java
org.muckebox.android.utils.Preferences.java
org.muckebox.android.utils.RemoteControlReceiver.java