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; /*from w w w .j a v a2s . c om*/ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextSwitcher; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.net.URI; import java.util.Properties; // TODO: add references: https://code.google.com/p/android-xmlrpc/, // TODO: Android Programming: The Big Nerd Ranch Guide by Brian Hardy and Bill Phillips // TODO: add description to this class public class MainActivity extends Activity { // For debugging private static final String TAG = "MainActivity"; private TextView status; private ListView tests; private TextSwitcher testResult; private Drawable errorDrawable; private Properties mProperties; private JSONArray array; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); testResult = (TextSwitcher) findViewById(R.id.text_result); LayoutInflater inflater = LayoutInflater.from(this); // TODO: what about those 2 views? View v0 = inflater.inflate(R.layout.text_view, null); View v1 = inflater.inflate(R.layout.text_view, null); ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); testResult.addView(v0, 0, layoutParams); testResult.addView(v1, 1, layoutParams); testResult.setText("WARNING, before calling any test make sure server.py is running !!!"); // What is Animation, AnimationUtils? Animation inAnim = AnimationUtils.loadAnimation(this, R.anim.push_left_in); Animation outAnim = AnimationUtils.loadAnimation(this, R.anim.push_left_out); inAnim.setStartOffset(250); testResult.setInAnimation(inAnim); testResult.setOutAnimation(outAnim); errorDrawable = getResources().getDrawable(R.drawable.error); errorDrawable.setBounds(0, 0, errorDrawable.getIntrinsicWidth(), errorDrawable.getIntrinsicHeight()); status = (TextView) findViewById(R.id.status); tests = (ListView) findViewById(R.id.tests); ArrayAdapter<String> adapter = new TestAdapter(this, R.layout.test, R.id.title); adapter.add("ping;out String"); adapter.add("time;out String"); adapter.add("get list of media files;out JSON"); adapter.add("WORK OFFLINE"); //adapter.add("get song; out bytes"); tests.setAdapter(adapter); tests.setOnItemClickListener(testListener); AssetsPropertyReader propertiesReader = new AssetsPropertyReader(this); mProperties = propertiesReader.getProperties("app.properties"); } AdapterView.OnItemClickListener testListener = new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { URI uri = URI.create(mProperties.getProperty("server_url")); // TODO: Do we use only one AsyncTask for all tasks or do we create an AsyncTask per // TODO: task like what we are doing? if (position == 0) { // getPing updateUI("Calling host " + uri.getHost()); new FetchItemsTask().execute(0); } else if (position == 1) { // getNow updateUI("Calling host " + uri.getHost()); new FetchItemsTask().execute(1); } else if (position == 2) { // getListFiles updateUI("Calling host " + uri.getHost()); new FetchItemsTask().execute(2); } else if (position == 3) { // Work offline updateUI("Reloading the media data from the database"); new FetchItemsTask().execute(3); } } }; // TODO: add description // TODO: call it preUpdateUI and there will be another similar function called postUpdateUI private void updateUI(String msg){ status.setTextColor(0xff80ff80); status.setError(null); status.setText(msg); tests.setEnabled(false); } // TODO: add description private class FetchItemsTask extends AsyncTask<Integer, Void, Void> { private long t0; private long t1; private String statusMessage; private String receivedResult; private boolean isSuccess; private int param; public FetchItemsTask() { super(); t0 = 0; t1 = 0; statusMessage = ""; receivedResult = ""; isSuccess = false; param = 0; } // Gets data from Streaming Server @Override protected Void doInBackground(Integer... params) { // TODO: Do something in onPostExecute param = params[0]; try { if (param == 0) { // getPing String message = getPing(); isSuccess = true; statusMessage = "HTTP call took " + (t1-t0) + "ms"; receivedResult = message; } else if (param == 1) { // getNow String message = getNow(); isSuccess = true; statusMessage = "HTTP call took " + (t1-t0) + "ms"; receivedResult = message; } else if (param == 2) { // getListFiles getListFiles(); // TODO: do we do 'MainActivity.this' or 'this' for getActivity()? // Start a MediaListActivity startActivity(new Intent(MainActivity.this, MediaListActivity.class)); isSuccess = true; statusMessage = "Success"; statusMessage = "HTTP call took " + (t1-t0) + "ms"; } else if (param == 3) { // Work offline // TODO: do we do 'MainActivity.this' or 'this' for getActivity()? // TODO: when we go back to the main page from MediaActivity, is the MediaActivity destroyed? // Start a MediaListActivity startActivity(new Intent(MainActivity.this, MediaListActivity.class)); isSuccess = true; statusMessage = "Success"; receivedResult = "List of files was successfully loaded"; } } catch (JSONException e) { // NOTE: The JSONException is associated with: new JSONObject, getJSONArray, // and getJSONObject in getListFiles() Log.e(TAG, e.getMessage()); statusMessage = e.getMessage(); } catch (IOException e) { Log.e(TAG, e.getMessage()); statusMessage = e.getMessage(); } return null; } private String getPing() throws IOException { t0 = System.currentTimeMillis(); String message = new DataFetcher(MainActivity.this).fetchData(mProperties.getProperty("ping_query")); t1 = System.currentTimeMillis(); return message; } private String getNow() throws IOException { t0 = System.currentTimeMillis(); String message = new DataFetcher(MainActivity.this).fetchData(mProperties.getProperty("time_query")); t1 = System.currentTimeMillis(); return message; } private void getListFiles() throws IOException, JSONException { t0 = System.currentTimeMillis(); String jsonString = new DataFetcher(MainActivity.this).fetchData(mProperties.getProperty("list_query")); t1 = System.currentTimeMillis(); // TODO: add comments. What are we doing here? JSONObject jsonObj = new JSONObject(jsonString); array = jsonObj.getJSONArray("media"); MediaManager mediaManager = MediaManager.get(MainActivity.this); // TODO: the execution time of the insert sql command is the same whether we use transactions or transactions+compiled sql statements //mediaManager.bulkInsertListOfMedia(array); mediaManager.insertListOfMedia(array); } @Override // TODO: add description // NOTE 1: It is run on the main thread, not the background thread like doInBackground() // does // NOTE 2: since it is run on the main thread, it is safe to update the UI within it. // You are not allowed to update the UI from a background thread because it can lead to // objects in memory becoming corrupted (the background thread and main thread want to both // update the UI at the same time) protected void onPostExecute(Void v) { // TODO: put the following code in a function called postUpdateUI tests.setEnabled(true); if (isSuccess) { status.setText(statusMessage); testResult.setText(receivedResult); } else { testResult.setText(""); status.setTextColor(0xffff8080); status.setError("", errorDrawable); status.setText("Fault message: " + statusMessage); } // TODO: you can remove the following commented code because we are not using AsyncQueryHandler anymore // TODO: to perform the insertions in the database /* if (param == 2) { int numberOfItemsAdded = 0; AsyncQueryHandler queryHandler = new AsyncQueryHandler( MainActivity.this.getContentResolver()) { @Override protected void onInsertComplete(int token, Object cookie, Uri uri) { super.onInsertComplete(token, cookie, uri); } }; for (int i = 0; i < array.length(); i++) { try { // TODO: move it in a separate thread Media media = new Media(array.getJSONObject(i)); // NOTE: ContentValues objects represent the mapping of column names to values ContentValues cv = new ContentValues(); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_ID, media.getMediaId()); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_FILENAME, media.getFilename()); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_TITLE, media.getTitle()); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_ARTIST, media.getArtist()); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_ALBUM, media.getAlbum()); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_LENGTH, media.getLength()); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_MINUTES, media.getMins()); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_SECONDS, media.getSecs()); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_ENCODER, media.getEncoder()); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_BITRATE, media.getBitrate()); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_SAMPLE_RATE, media.getSampleRate()); cv.put(MediaDatabaseHelper.COLUMN_MEDIA_LAYER, media.getLayer()); // TODO: add the year and date in the media table //cv.put(MediaDatabaseHelper.COLUMN_MEDIA_YEAR, media.getYear()); //cv.put(MediaDatabaseHelper.COLUMN_MEDIA_DATE, media.getDate()); // TODO: there is a NULLPointerException when trying to retrieve the photo filename // TODO: Photo is null when a Media object is created //cv.put(MediaDatabaseHelper.COLUMN_MEDIA_PHOTO, media.getPhoto().getFilename()); queryHandler.startInsert(0, null, MediaProvider.CONTENT_URI, cv); } catch (JSONException e) { Log.e(TAG, e.getMessage()); } } // TODO: do we do 'MainActivity.this' or 'this' for getActivity()? // Start a MediaListActivity Intent i = new Intent(MainActivity.this, MediaListActivity.class); startActivity(i); } */ } } class TestAdapter extends ArrayAdapter<String> { private LayoutInflater layouter; private int layoutId; public TestAdapter(Context context, int layoutId, int textId) { super(context, layoutId, textId); this.layoutId = layoutId; layouter = LayoutInflater.from(MainActivity.this); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = layouter.inflate(layoutId, null); TextView title = (TextView) view.findViewById(R.id.title); TextView params = (TextView) view.findViewById(R.id.params); String string = getItem(position); String[] arr = string.split(";"); title.setText(arr[0]); if (arr.length == 2) { params.setText(arr[1]); } else { params.setVisibility(View.GONE); } return view; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }