Back to project page streaming_project.
The source code is released under:
GNU General Public License
If you think the Android project streaming_project listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.streaming.streaming; // w w w . ja va2s. com import android.content.Context; import android.util.Log; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; // TODO: why do we need this class? Can we just use directly MediaDatabaseHelper instead? // MediaManager will create a storage mechanism for the Streaming app that uses a database to store // data about media. // NOTE: it provides a database API that the rest of the app uses to store and retrieve data public class MediaManager { // For debugging private static final String TAG = "MediaManager"; private static MediaManager sMediaManager; private Context mAppContext; private MediaDatabaseHelper mHelper; // The private constructor forces users to use MediaManager.get(Context) private MediaManager(Context appContext) { mAppContext = appContext; mHelper = new MediaDatabaseHelper(mAppContext); // NOTE: every time the app is started, I drop the media Table if it exists and re-create it // I do this because I don't want to add the same data over and over again after I start the // app // TODO: see if I am doing right. See http://stackoverflow.com/questions/20192642/android-onupgrade-not-calling-at-database-upgrade, http://stackoverflow.com/questions/7647566/why-is-onupgrade-not-being-invoked-on-android-sqlite-database // TODO: if we go back to MainActivity from MediaListFragment, and then we press the getListFiles button, we will add the same data again in the db // TODO: because we are not calling this constructor which destroys the db and re-create it. mHelper.onUpgrade(mHelper.getReadableDatabase(), 1, 2); } public static MediaManager get(Context c) { if (sMediaManager == null) { // Use the application context to avoid leaking activities sMediaManager = new MediaManager(c.getApplicationContext()); } return sMediaManager; } // Creates and inserts a new Media into the database. More precisely, the JSONObject // representing a media file will be used to create an instance of Media that will then be added // to the database. // TODO: not used anymore. Been replaced by insertListOfMedia. public void insertMedia(JSONObject obj) throws JSONException { Media media = new Media(obj); mHelper.insertMedia(media); } // TODO: add description public void insertListOfMedia(JSONArray array) throws JSONException { long startTime = System.currentTimeMillis(); mHelper.getWritableDatabase().beginTransaction(); // Convert the JSONObject into an instance of Media and insert the Media object into the // database. TODO: Should we use a loader to do the work? // TODO: When inserting the media info into the database, the app is not responsive. It freezes while the database is being filled for (int i = 0; i < array.length(); i++) { Media media = new Media(array.getJSONObject(i)); mHelper.insertMedia(media); } mHelper.getWritableDatabase().setTransactionSuccessful(); mHelper.getWritableDatabase().endTransaction(); long diff = System.currentTimeMillis() - startTime; Log.d(TAG, "Exec Time: " + Long.toString(diff) + "ms"); } // TODO: add description public void bulkInsertListOfMedia(JSONArray array) throws JSONException { long startTime = System.currentTimeMillis(); mHelper.getWritableDatabase().beginTransaction(); // Convert the JSONObject into an instance of Media and insert the Media object into the // database. TODO: Should we use a loader to do the work? // TODO: When inserting the media info into the database, the app is not responsive. It freezes while the database is being filled for (int i = 0; i < array.length(); i++) { Media media = new Media(array.getJSONObject(i)); mHelper.insertMedia(media); } mHelper.getWritableDatabase().setTransactionSuccessful(); mHelper.getWritableDatabase().endTransaction(); long diff = System.currentTimeMillis() - startTime; Log.d(TAG, "Exec Time: " + Long.toString(diff) + "ms"); } // Proxy media queries // NOTE: it is called in MediaListFragment public MediaDatabaseHelper.MediaCursor queryMediaList() { return mHelper.queryMediaList(); } // Pulls a media out of the first row of the MediaCursor retrieved from queryMedia() public Media getMedia(long id) { Media media = null; MediaDatabaseHelper.MediaCursor cursor = mHelper.queryMedia(id); // Move the MediaCursor to the first row in the results cursor.moveToFirst(); // If you got a row, get a media // NOTE: isAfterLast() returns false if there are any results at all if (!cursor.isAfterLast()) // Since there is a result, we can safely ask for a Media for this row media = cursor.getMedia(); // NOTE: Since the caller of this method will not have access to the MediaCursor, you must // be careful to call close() on it before returning, so that the database can release any // resources associated with the cursor from memory as soon as possible. cursor.close(); return media; } }