Commit e85b6168 authored by Alan McNatty's avatar Alan McNatty
Browse files

Added logging class and wrapper for debug / verbose logging so they are...

Added logging class and wrapper for debug / verbose logging so they are excluded from non-debug builds
parent f9f35580
......@@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nz.net.catalyst.MaharaDroid"
android:versionCode="1"
android:versionName="1.0">
android:versionName="1.1">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="false">
<activity android:name=".MaharaDroid"
......
......@@ -56,6 +56,7 @@
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
<!--
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ArtifactFolderLayout"
......@@ -77,7 +78,7 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
</LinearLayout> -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ArtifactSettingsButtonLayout"
......
......@@ -35,6 +35,12 @@ import android.os.Bundle;
public class ArtifactSendReceiver extends Activity {
static final String TAG = LogConfig.getLogTag(ArtifactSendReceiver.class);
// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
static final boolean DEBUG = LogConfig.isDebug(TAG);
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -46,7 +52,6 @@ public class ArtifactSendReceiver extends Activity {
Uri uri = (Uri)extras.get("android.intent.extra.STREAM");
Intent i = new Intent(this, ArtifactSettings.class);
i.putExtra("uri", uri.toString());
//i.putExtra("action", "upload");
startActivity(i);
}
}
......
......@@ -26,7 +26,6 @@ import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
......@@ -45,6 +44,12 @@ import android.widget.Toast;
public class ArtifactSettings extends Activity implements OnClickListener {
static final String TAG = LogConfig.getLogTag(ArtifactSettings.class);
// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
static final boolean DEBUG = LogConfig.isDebug(TAG);
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
private Bundle m_extras;
private String m_filepath = null;
......@@ -53,7 +58,7 @@ public class ArtifactSettings extends Activity implements OnClickListener {
super.onCreate(savedInstanceState);
// ensure we have default preferences (if none set by user)
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
//PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
//TODO: Add thumbnail of image to upload options screen.
setContentView(R.layout.artifact_settings);
......@@ -86,7 +91,6 @@ public class ArtifactSettings extends Activity implements OnClickListener {
String title = m_filepath.substring(m_filepath.lastIndexOf("/") + 1,
m_filepath.lastIndexOf("."));
((EditText)findViewById(R.id.txtArtifactTitle)).setText(title);
//((EditText)findViewById(R.id.txtArtifactTitle)).selectAll();
}
}
}
......@@ -103,7 +107,6 @@ public class ArtifactSettings extends Activity implements OnClickListener {
// upload in the Extras Bundle.
Intent uploader_intent = new Intent(this, TransferService.class);
uploader_intent.putExtra("filename", m_filepath);
//uploader_intent.putExtra("type", "upload");
uploader_intent.putExtra("title", ((EditText)findViewById(R.id.txtArtifactTitle)).getText().toString());
// Start the uploader service and pass in the intent containing
......
......@@ -35,7 +35,12 @@ import android.view.MenuItem;
public class EditPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
static final String TAG = LogConfig.getLogTag(EditPreferences.class);
// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
static final boolean DEBUG = LogConfig.isDebug(TAG);
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -79,10 +84,17 @@ public class EditPreferences extends PreferenceActivity implements OnSharedPrefe
}
}
//public void onContentChanged() {
//}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// refresh displayed values by restarting activity (a hack, but apparently there
// isn't a nicer way)
finish();
//startActivity(getIntent());
}
private void showAboutPage() {
startActivity(new Intent(this, AboutActivity.class));
......
......@@ -31,13 +31,13 @@ package nz.net.catalyst.MaharaDroid;
public class GlobalResources {
public static final String INTENT_UPLOAD_STARTED = "nz.co.catalyst.MaharaDroid.UPLOAD_STARTED";
public static final String INTENT_UPLOAD_FINISHED = "nz.co.catalyst.MaharaDroid.UPLOAD_FINISHED";
public static final String INTENT_UPLOAD_FAILED = "nz.co.catalyst.MaharaDroid.UPLOAD_FAILED";
public static final String INTENT_BIND_TRANSFER_SERVICE = "nz.co.catalyst.MaharaDroid.BIND_TRANSFER_SERVICE";
public static final String INTENT_BIND_DOWNLOADER = "nz.co.catalyst.MaharaDroid.BIND_DOWNLOADER";
public static final String INTENT_UPLOAD_PROGRESS_UPDATE = "nz.co.catalyst.MaharaDroid.UPLOAD_PROGRESS_UPDATE";
public static final String INTENT_DOWNLOAD_PROGRESS_UPDATE = "nz.co.catalyst.MaharaDroid.DOWNLOAD_PROGRESS_UPDATE";
public static final String INTENT_UPLOAD_STARTED = "nz.net.catalyst.MaharaDroid.UPLOAD_STARTED";
public static final String INTENT_UPLOAD_FINISHED = "nz.net.catalyst.MaharaDroid.UPLOAD_FINISHED";
public static final String INTENT_UPLOAD_FAILED = "nz.net.catalyst.MaharaDroid.UPLOAD_FAILED";
public static final String INTENT_BIND_TRANSFER_SERVICE = "nz.net.catalyst.MaharaDroid.BIND_TRANSFER_SERVICE";
public static final String INTENT_BIND_DOWNLOADER = "nz.net.catalyst.MaharaDroid.BIND_DOWNLOADER";
public static final String INTENT_UPLOAD_PROGRESS_UPDATE = "nz.net.catalyst.MaharaDroid.UPLOAD_PROGRESS_UPDATE";
public static final String INTENT_DOWNLOAD_PROGRESS_UPDATE = "nz.net.catalyst.MaharaDroid.DOWNLOAD_PROGRESS_UPDATE";
public static final String TRANSFER_TYPE_UPLOAD = "Upload";
......
......@@ -42,7 +42,12 @@ import android.util.Log;
*/
public class MultipartEntityMonitored extends MultipartEntity {
static final String TAG = LogConfig.getLogTag(MultipartEntityMonitored.class);
// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
static final boolean DEBUG = LogConfig.isDebug(TAG);
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
public class OutputStreamMonitored extends FilterOutputStream {
public OutputStreamMonitored(OutputStream out, long length) {
......@@ -137,7 +142,7 @@ public class MultipartEntityMonitored extends MultipartEntity {
@Override
public void writeTo(OutputStream outstream) throws IOException {
Log.d("UPLOAD", "Uploading data");
if ( DEBUG ) Log.d(TAG, "Uploading data");
if (m_outputstream == null) {
m_outputstream = new OutputStreamMonitored(outstream, getContentLength());
}
......
......@@ -64,7 +64,11 @@ import android.util.Log;
*/
public class RestClient {
static final String TAG = LogConfig.getLogTag(RestClient.class);
// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
static final boolean DEBUG = LogConfig.isDebug(TAG);
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
private static final int CONNECTION_TIMEOUT = 15000000;
private static String convertStreamToString(InputStream is) {
......@@ -106,18 +110,15 @@ public class RestClient {
pNames.add("title");
pVals.add(title);
}
/*
pNames.add("content_type");
pVals.add("1");
pNames.add("hidden");
pVals.add("1");
*/
String [] paramNames, paramVals;
paramNames = paramVals = new String[]{};
paramNames = pNames.toArray(paramNames);
paramVals = pVals.toArray(paramVals);
//Log.d("MaharaDroid", "String url: '" + url + "', String token: '" + token + "', String folder: '" + folder + "', String filename: '" + filename + "'");
if ( DEBUG ) Log.d(TAG, "String url: '" + url +
"', String token: '" + token +
"', String folder: '" + folder +
"', String filename: '" + filename + "'");
return CallFunction(url, token, folder, filename, paramNames, paramVals, context);
}
......@@ -128,8 +129,11 @@ public class RestClient {
{
JSONObject json = new JSONObject();
SchemeRegistry supportedSchemes = new SchemeRegistry();
// Register the "http" and "https" protocol schemes, they are
// required by the default operator to look up socket factories.
//TODO we make assumptions about ports.
supportedSchemes.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
supportedSchemes.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
......@@ -144,7 +148,10 @@ public class RestClient {
DefaultHttpClient httpclient = new DefaultHttpClient(ccm, http_params);
//Log.d("MaharaDroid", "String url: '" + url + "', String token: '" + token + "', String folder: '" + folder + "', String filename: '" + filename + "'");
if ( DEBUG ) Log.d(TAG, "String url: '" + url +
"', String token: '" + token +
"', String folder: '" + folder +
"', String filename: '" + filename + "'");
if (paramNames == null) {
paramNames = new String[0];
......@@ -154,7 +161,7 @@ public class RestClient {
}
if (paramNames.length != paramVals.length) {
Log.w("MaharaDroid", "Incompatible nuber of param names and values, bailing on upload!");
Log.w(TAG, "Incompatible nuber of param names and values, bailing on upload!");
return json;
}
......@@ -197,30 +204,28 @@ public class RestClient {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
//resEntity.consumeContent();
String content = convertStreamToString(resEntity.getContent());
// TODO analyse content and put success / fail
if ( response.getStatusLine().getStatusCode() == 200 ) {
try {
json.put("success", content.toString());
Log.i(TAG, "success, updating token to : " + content.toString());
} catch (JSONException e) {
json = null;
}
} else {
Log.w("MaharaDroid", "File upload failed with response code:" + response.getStatusLine().getStatusCode());
Log.w(TAG, "File upload failed with response code:" + response.getStatusLine().getStatusCode());
try {
json.put("fail", "Artefact upload failed");
Log.d("MaharaDroid", "HTTP POST returned status code: " + response.getStatusLine());
Log.d("MaharaDroid", content);
json.put("fail", response.getStatusLine().getReasonPhrase());
if ( DEBUG) Log.d(TAG, "HTTP POST returned status code: " + response.getStatusLine());
} catch (JSONException e) {
json = null;
}
}
} else {
Log.w("MaharaDroid", "Response does not contain a valid HTTP entity.");
Log.w(TAG, "Response does not contain a valid HTTP entity.");
try {
json.put("fail", "Artefact upload failed");
Log.d("MaharaDroid", "HTTP POST returned status code: " + response.getStatusLine());
if ( DEBUG ) Log.d(TAG, "HTTP POST returned status code: " + response.getStatusLine());
} catch (JSONException e) {
json = null;
}
......
......@@ -52,7 +52,12 @@ import android.content.ServiceConnection;
*/
public class TransferProgress extends Activity implements OnClickListener {
static final String TAG = LogConfig.getLogTag(TransferProgress.class);
// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
static final boolean DEBUG = LogConfig.isDebug(TAG);
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
private StatusReceiver m_receiver = null;
private BindTransferServiceReceiver m_bind_transfer_service_receiver = null;
private PercentProgressUpdateReceiver m_update_receiver = null;
......
......@@ -34,7 +34,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.Bundle;
......@@ -53,8 +52,13 @@ import android.widget.Toast;
* @author Alan McNatty (alan.mcnatty@catalyst.net.nz)
*/
public class TransferService extends Service implements OnSharedPreferenceChangeListener {
public class TransferService extends Service {
static final String TAG = LogConfig.getLogTag(TransferService.class);
// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
static final boolean DEBUG = LogConfig.isDebug(TAG);
// whether VERBOSE level logging is enabled
static final boolean VERBOSE = LogConfig.VERBOSE;
private Notification m_upload_notification = null;
private NotificationProgressUpdateReceiver m_update_receiver = null;
private PendingIntent m_notify_activity = null;
......@@ -68,7 +72,6 @@ public class TransferService extends Service implements OnSharedPreferenceChange
super.onCreate();
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.registerOnSharedPreferenceChangeListener(this);
}
// This is the receiver that we use to update the percentage progress display
......@@ -124,6 +127,12 @@ public class TransferService extends Service implements OnSharedPreferenceChange
}
publishProgress("fail", err_str);
m_uploads.clear();
} else if ( result.has("success") ) {
try {
setUploadAuthTokenPref(result.getString("success").trim());
} catch (JSONException e) {
Log.e(TAG, "Failed to get success token from result.");
}
}
}
}
......@@ -182,16 +191,6 @@ public class TransferService extends Service implements OnSharedPreferenceChange
Toast.makeText(getApplicationContext(), R.string.uploadfinished, Toast.LENGTH_SHORT).show();
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).cancel(GlobalResources.UPLOADER_ID);
// Check the result - if a new token is provided, update our preferences.
if ( result != null ) {
try {
setUploadAuthTokenPref(((JSONObject) result).getString("success"));
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.w("MaharaDroid", "Hmm .. got success but not a valid token");
}
}
stopSelf();
}
......@@ -218,7 +217,7 @@ public class TransferService extends Service implements OnSharedPreferenceChange
@Override
public void onDestroy () {
super.onDestroy();
mPrefs.unregisterOnSharedPreferenceChangeListener(this);
//mPrefs.unregisterOnSharedPreferenceChangeListener(this);
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).cancel(GlobalResources.UPLOADER_ID);
if (m_update_receiver != null) {
this.unregisterReceiver(m_update_receiver);
......@@ -282,12 +281,6 @@ public class TransferService extends Service implements OnSharedPreferenceChange
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
}
private String getUploadURLPref() {
return mPrefs.getString(getString(R.string.pref_upload_url_key), null);
}
......@@ -295,11 +288,13 @@ public class TransferService extends Service implements OnSharedPreferenceChange
return mPrefs.getString(getString(R.string.pref_upload_folder_key), null);
}
public String getUploadAuthTokenPref() {
//this.getDeviceId()
return mPrefs.getString(getString(R.string.pref_upload_token_key), null);
}
public void setUploadAuthTokenPref(String newToken) {
Log.d("MaharaDroid", "New Token is '" + newToken + "'");
mPrefs.edit().putString(getString(R.string.pref_upload_token_key), newToken);
if ( DEBUG ) Log.d(TAG, "New Token is '" + newToken + "'");
mPrefs.edit()
.putString(getString(R.string.pref_upload_token_key), newToken)
.commit()
;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment