Commit 3cda0a77 authored by Alan McNatty's avatar Alan McNatty
Browse files

Bunch of changes .. now using user defined token and testing input of folder.

parent b717d725
......@@ -12,9 +12,8 @@ public final class R {
}
public static final class drawable {
public static final int icon=0x7f020000;
public static final int maharadroid=0x7f020001;
public static final int menu_info=0x7f020002;
public static final int menu_revert=0x7f020003;
public static final int menu_info=0x7f020001;
public static final int menu_revert=0x7f020002;
}
public static final class id {
public static final int ArtifactConfirmLayout=0x7f080004;
......@@ -86,13 +85,13 @@ public final class R {
public static final int pref_upload_folder_key=0x7f06000a;
public static final int pref_upload_folder_summary=0x7f06000c;
public static final int pref_upload_folder_title=0x7f06000b;
public static final int pref_upload_https_key=0x7f060007;
public static final int pref_upload_https_summary=0x7f060009;
public static final int pref_upload_https_title=0x7f060008;
public static final int pref_upload_token_key=0x7f060006;
public static final int pref_upload_token_summary=0x7f060008;
public static final int pref_upload_token_title=0x7f060007;
public static final int pref_upload_url_key=0x7f060003;
public static final int pref_upload_url_summary=0x7f060005;
public static final int pref_upload_url_title=0x7f060004;
public static final int prefcat_advanced=0x7f060006;
public static final int prefcat_advanced=0x7f060009;
public static final int prefs_app_name=0x7f060002;
public static final int startup_text=0x7f060001;
public static final int transferprogress=0x7f060016;
......
......@@ -7,13 +7,13 @@
<string name="pref_upload_url_key">upload.uri</string>
<string name="pref_upload_url_title">Upload URI</string>
<string name="pref_upload_url_summary">Enter the URL to post files to Mahara</string>
<string name="pref_upload_token_key">upload.token</string>
<string name="pref_upload_token_title">Token</string>
<string name="pref_upload_token_summary">Enter your Mahara upload token</string>
<string name="prefcat_advanced">Advanced</string>
<string name="pref_upload_https_key">upload.https</string>
<string name="pref_upload_https_title">Enable HTTPs upload</string>
<string name="pref_upload_https_summary">Upload your artefacts over HTTPS</string>
<string name="pref_upload_folder_key">upload.folder</string>
<string name="pref_upload_folder_title">Upload Folder</string>
<string name="pref_upload_folder_summary">Enter the folder to upload your artefacts into</string>
......
......@@ -7,8 +7,12 @@
android:key="@string/pref_upload_url_key"
android:title="@string/pref_upload_url_title"
android:summary="@string/pref_upload_url_summary"
android:defaultValue="http://10.0.1.100/mahara/artefact/file/mobileupload.php" />
android:defaultValue="http://www.mahara.org/artefact/file/mobileupload.php" />
<EditTextPreference
android:key="@string/pref_upload_token_key"
android:title="@string/pref_upload_token_title"
android:summary="@string/pref_upload_token_summary" />
<PreferenceCategory
android:title="@string/prefcat_advanced">
......@@ -16,7 +20,6 @@
<EditTextPreference
android:key="@string/pref_upload_folder_key"
android:title="@string/pref_upload_folder_title"
android:enabled="false"
android:summary="@string/pref_upload_folder_summary"
android:defaultValue="Home" />
......
......@@ -103,7 +103,7 @@ 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("type", "upload");
uploader_intent.putExtra("title", ((EditText)findViewById(R.id.txtArtifactTitle)).getText().toString());
// Start the uploader service and pass in the intent containing
......
......@@ -29,6 +29,7 @@ import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
......@@ -43,6 +44,12 @@ public class EditPreferences extends PreferenceActivity implements OnSharedPrefe
addPreferencesFromResource(R.xml.preferences);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if ( prefs.getString(getString(R.string.pref_upload_token_key), null).trim().length() == 0 ) {
// set the token to the IMEI is null
prefs.edit().putString(getString(R.string.pref_upload_token_key), this.getDeviceId());
}
prefs.registerOnSharedPreferenceChangeListener(this);
}
......@@ -94,10 +101,17 @@ public class EditPreferences extends PreferenceActivity implements OnSharedPrefe
// reset defaults
PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
// set the token to the IMEI is null
prefs.edit().putString(getString(R.string.pref_upload_token_key), this.getDeviceId());
// refresh displayed values by restarting activity (a hack, but apparently there
// isn't a nicer way)
finish();
startActivity(getIntent());
}
public String getDeviceId() {
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
}
}
......@@ -95,8 +95,8 @@ public class RestClient {
return sb.toString();
}
public static JSONObject UploadArtifact(String url, String id, String filename,
String title, Context context){
public static JSONObject UploadArtifact(String url, String token, String folder,
String filename, String title, Context context){
Vector<String> pNames = new Vector<String>();
Vector<String> pVals = new Vector<String>();
......@@ -117,12 +117,14 @@ public class RestClient {
paramNames = pNames.toArray(paramNames);
paramVals = pVals.toArray(paramVals);
return CallFunction(url, id, paramNames, paramVals, filename, context);
Log.d("MaharaDroid", "String url: '" + url + "', String token: '" + token + "', String folder: '" + folder + "', String filename: '" + filename + "'");
return CallFunction(url, token, folder, filename, paramNames, paramVals, context);
}
public static JSONObject CallFunction(String url, String id,
public static JSONObject CallFunction(String url, String token, String folder, String filename,
String[] paramNames, String[] paramVals,
String filename, Context context)
Context context)
{
JSONObject json = new JSONObject();
SchemeRegistry supportedSchemes = new SchemeRegistry();
......@@ -142,6 +144,7 @@ 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 (paramNames == null) {
paramNames = new String[0];
......@@ -155,51 +158,11 @@ public class RestClient {
return json;
}
// TODO this all needs configuring...
/*
URL mURL;
try {
mURL = new URL(url);
} catch (MalformedURLException e2) {
Log.w("MaharaDroid", "Malformed URL '" + url + "', bailing on upload!");
return json;
}
String mDomain = mURL.getHost();
String mPath = mURL.getPath();
*/
SortedMap<String,String> sig_params = new TreeMap<String,String>();
sig_params.put("id", id);
sig_params.put("folder", "0");
sig_params.put("token", token);
sig_params.put("foldername", folder);
/*
sig_params.put("JSON", "1");
sig_params.put("NOSESSKEY", "1");
sig_params.put("files_filebrowser_changefolder", "");
sig_params.put("files_filebrowser_foldername", "Home");
sig_params.put("files_filebrowser_uploadnumber", "1");
sig_params.put("files_filebrowser_upload", "1");
sig_params.put("MAX_FILE_SIZE", "2097152");
sig_params.put("files_filebrowser_notice", "on");
sig_params.put("pieform_files", "1");
sig_params.put("pieform_jssubmission", "1");
SortedMap<String,String> m_cookies = new TreeMap<String,String>();
m_cookies.put("mahara", token);
m_cookies.put("ctest", "1");
BasicCookieStore mCookieStore = new BasicCookieStore();
for (Map.Entry<String,String> entry : m_cookies.entrySet()) {
BasicClientCookie c = new BasicClientCookie(entry.getKey(), entry.getValue());
c.setDomain(mDomain);
c.setPath(mPath);
mCookieStore.addCookie(c);
}
httpclient.setCookieStore(mCookieStore);
*/
HttpResponse response = null;
try {
......@@ -237,15 +200,14 @@ public class RestClient {
//resEntity.consumeContent();
String content = convertStreamToString(resEntity.getContent());
// TODO analyse content and put success / fail
if ( response.getStatusLine().getStatusCode() == 200 &&
content.indexOf("success") >= 0) {
if ( response.getStatusLine().getStatusCode() == 200 ) {
try {
json.put("success", "Artefact successfully uploaded");
json.put("success", content.toString());
} catch (JSONException e) {
json = null;
}
} else {
Log.w("MaharaDroid", "File upload success could not be determined (non 200 or error in response body).");
Log.w("MaharaDroid", "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());
......
......@@ -53,6 +53,11 @@ import android.content.ServiceConnection;
public class TransferProgress extends Activity implements OnClickListener {
private StatusReceiver m_receiver = null;
private BindTransferServiceReceiver m_bind_transfer_service_receiver = null;
private PercentProgressUpdateReceiver m_update_receiver = null;
private TransferService m_transfer_service = null;
private ServiceConnection m_svc = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
......@@ -69,7 +74,7 @@ public class TransferProgress extends Activity implements OnClickListener {
};
// This is the receiver that we use to update the percentage progress display
// for the current upload and/or download.
// for the current upload
public class PercentProgressUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
......@@ -194,14 +199,14 @@ public class TransferProgress extends Activity implements OnClickListener {
ArrayList < HashMap<String, String> > transferlist = new ArrayList < HashMap<String,String> >();
LinkedList<Bundle> upload_list = m_transfer_service.getUploads();
// If either the upload or download lists is empty, then be sure to cancel the appropriate
// notification. If both are empty, then there's nothing to do here, so close the TransferProgress
// If the upload list is empty, then be sure to cancel the appropriate
// notification. If it is empty, then there's nothing to do here, so close the TransferProgress
// Activity.
if (upload_list.isEmpty()) {
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).cancel(GlobalResources.UPLOADER_ID);
}
// We want to interleave the list of uploads and downloads in the ListView.
// We want to interleave the list of uploads in the ListView.
HashMap<String, String> m;
Bundle b = null;
while (!upload_list.isEmpty() ) {
......@@ -229,8 +234,4 @@ public class TransferProgress extends Activity implements OnClickListener {
}
}
private StatusReceiver m_receiver = null;
private BindTransferServiceReceiver m_bind_transfer_service_receiver = null;
private PercentProgressUpdateReceiver m_update_receiver = null;
private TransferService m_transfer_service = null;
}
......@@ -40,7 +40,7 @@ import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
......@@ -105,9 +105,10 @@ public class TransferService extends Service implements OnSharedPreferenceChange
upload_info = m_uploads.get(0);
if (upload_info != null) {
publishProgress(new String[]{"start", upload_info.getString("title")});
JSONObject result = RestClient.UploadArtifact(
getUploadURLPref(), getDeviceId(),
getUploadURLPref(),
getUploadAuthTokenPref(),
getUploadFolderPref(),
upload_info.getString("filename"),
upload_info.getString("title"),
getApplicationContext());
......@@ -154,7 +155,7 @@ public class TransferService extends Service implements OnSharedPreferenceChange
getApplicationContext().sendBroadcast(broadcast_intent);
}
if (status.equals("finish")) {
// Send out a broadcast to let us know that an upload is starting.
// Send out a broadcast to let us know that an upload is finished.
broadcast_intent.setAction(GlobalResources.INTENT_UPLOAD_FINISHED);
getApplicationContext().sendBroadcast(broadcast_intent);
}
......@@ -180,6 +181,17 @@ public class TransferService extends Service implements OnSharedPreferenceChange
// Uploader service.
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();
}
......@@ -217,9 +229,7 @@ public class TransferService extends Service implements OnSharedPreferenceChange
public void onStart(Intent intent, int startId) {
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.getString("type").equals("upload")) {
addUpload(intent.getExtras());
}
addUpload(intent.getExtras());
}
m_update_receiver = new NotificationProgressUpdateReceiver();
if (m_update_receiver != null) {
......@@ -281,9 +291,15 @@ public class TransferService extends Service implements OnSharedPreferenceChange
private String getUploadURLPref() {
return mPrefs.getString(getString(R.string.pref_upload_url_key), null);
}
public String getDeviceId() {
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
private String getUploadFolderPref() {
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);
}
}
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