mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Add proper login flow
This commit is contained in:
parent
2391cfbc1b
commit
43d0f6a404
6 changed files with 125 additions and 11 deletions
|
|
@ -27,7 +27,6 @@
|
||||||
android:background="@drawable/share_overlay_background_gradient" >
|
android:background="@drawable/share_overlay_background_gradient" >
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/overlayTitle"
|
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="@string/upload_overlay_title"
|
android:text="@string/upload_overlay_title"
|
||||||
|
|
@ -42,7 +41,7 @@
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/overlayTitleEdit"
|
android:id="@+id/titleEdit"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:singleLine="true"
|
android:singleLine="true"
|
||||||
|
|
@ -54,17 +53,17 @@
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Description"
|
android:text="Description"
|
||||||
android:textAppearance="?android:attr/textAppearanceMedium"
|
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||||
android:layout_below="@id/overlayTitle"/>
|
/>
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/overlayDescEdit"
|
android:id="@+id/descEdit"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:inputType="textMultiLine"
|
android:inputType="textMultiLine"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/overlayUploadButton"
|
android:id="@+id/uploadButton"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Upload"
|
android:text="Upload"
|
||||||
|
|
|
||||||
|
|
@ -12,5 +12,7 @@
|
||||||
<string name="logging_in_message">Witty text here</string>
|
<string name="logging_in_message">Witty text here</string>
|
||||||
<string name="title_activity_share">ShareActivity</string>
|
<string name="title_activity_share">ShareActivity</string>
|
||||||
<string name="upload_overlay_title">Upload to Commons</string>
|
<string name="upload_overlay_title">Upload to Commons</string>
|
||||||
|
<string name="login_success">Login success!</string>
|
||||||
|
<string name="login_failed">Login failed!</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
@ -13,7 +13,7 @@ public class CommonsApplication extends Application {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
DefaultHttpClient client = new DefaultHttpClient();
|
DefaultHttpClient client = new DefaultHttpClient();
|
||||||
api = new MWApi("http://test.wikipedia.org/w/api.php", client);
|
api = new MWApi("http://192.168.1.34/w/api.php", client);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MWApi getApi() {
|
public MWApi getApi() {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package org.wikimedia.commons;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.graphics.*;
|
import android.graphics.*;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.*;
|
import android.os.*;
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import android.content.Context;
|
||||||
import android.view.*;
|
import android.view.*;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
|
import android.widget.Toast;
|
||||||
import android.support.v4.app.NavUtils;
|
import android.support.v4.app.NavUtils;
|
||||||
|
|
||||||
public class LoginActivity extends Activity {
|
public class LoginActivity extends Activity {
|
||||||
|
|
@ -22,15 +23,19 @@ public class LoginActivity extends Activity {
|
||||||
|
|
||||||
private class LoginTask extends AsyncTask<String, String, String> {
|
private class LoginTask extends AsyncTask<String, String, String> {
|
||||||
|
|
||||||
Context context;
|
Activity context;
|
||||||
ProgressDialog dialog;
|
ProgressDialog dialog;
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(String result) {
|
protected void onPostExecute(String result) {
|
||||||
super.onPostExecute(result);
|
super.onPostExecute(result);
|
||||||
if(result.equals("Success")) {
|
if(result.equals("Success")) {
|
||||||
dialog.cancel();
|
dialog.cancel();
|
||||||
|
Toast successToast = Toast.makeText(context, R.string.login_success, Toast.LENGTH_SHORT);
|
||||||
|
successToast.show();
|
||||||
|
context.finish();
|
||||||
} else {
|
} else {
|
||||||
dialog.setMessage("Ayyooo poche!");
|
Toast failureToast = Toast.makeText(context, R.string.login_failed, Toast.LENGTH_LONG);
|
||||||
|
failureToast.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -45,7 +50,7 @@ public class LoginActivity extends Activity {
|
||||||
dialog.show();
|
dialog.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
LoginTask(Context context) {
|
LoginTask(Activity context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -71,7 +76,7 @@ public class LoginActivity extends Activity {
|
||||||
loginButton = (Button)findViewById(R.id.loginButton);
|
loginButton = (Button)findViewById(R.id.loginButton);
|
||||||
usernameEdit = (EditText)findViewById(R.id.loginUsername);
|
usernameEdit = (EditText)findViewById(R.id.loginUsername);
|
||||||
passwordEdit = (EditText)findViewById(R.id.loginPassword);
|
passwordEdit = (EditText)findViewById(R.id.loginPassword);
|
||||||
final Context that = this;
|
final Activity that = this;
|
||||||
loginButton.setOnClickListener(new View.OnClickListener() {
|
loginButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,27 @@ package org.wikimedia.commons;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
import javax.xml.transform.Transformer;
|
||||||
|
import javax.xml.transform.TransformerConfigurationException;
|
||||||
|
import javax.xml.transform.TransformerFactory;
|
||||||
|
import javax.xml.transform.TransformerFactoryConfigurationError;
|
||||||
|
import javax.xml.transform.dom.DOMSource;
|
||||||
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
|
||||||
|
import org.mediawiki.api.ApiResult;
|
||||||
|
import org.mediawiki.api.MWApi;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.*;
|
import android.graphics.*;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.*;
|
import android.view.*;
|
||||||
import android.widget.*;
|
import android.widget.*;
|
||||||
import android.support.v4.app.NavUtils;
|
import android.support.v4.app.NavUtils;
|
||||||
|
|
@ -18,22 +32,115 @@ public class ShareActivity extends Activity {
|
||||||
private CommonsApplication app;
|
private CommonsApplication app;
|
||||||
|
|
||||||
private ImageView backgroundImageView;
|
private ImageView backgroundImageView;
|
||||||
|
private Button uploadButton;
|
||||||
|
private EditText titleEdit;
|
||||||
|
private EditText descEdit;
|
||||||
|
|
||||||
|
private Uri imageUri;
|
||||||
|
|
||||||
|
class UploadImageTask extends AsyncTask<String, Integer, String> {
|
||||||
|
MWApi api;
|
||||||
|
Context context;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String doInBackground(String... params) {
|
||||||
|
Uri imageUri = Uri.parse(params[0]);
|
||||||
|
String filename = params[1];
|
||||||
|
String text = params[2];
|
||||||
|
String comment = params[3];
|
||||||
|
|
||||||
|
InputStream file;
|
||||||
|
|
||||||
|
try {
|
||||||
|
file = context.getContentResolver().openInputStream(imageUri);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("Commons", "isLoggedIn? " + api.isLoggedIn);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ApiResult result = api.upload(filename, file, text, comment);
|
||||||
|
Document document = (Document)result.getDocument();
|
||||||
|
StringWriter wr = new StringWriter();
|
||||||
|
try {
|
||||||
|
Transformer trans = TransformerFactory.newInstance().newTransformer();
|
||||||
|
trans.transform(new DOMSource(result.getDocument()), new StreamResult(wr));
|
||||||
|
String res = wr.toString();
|
||||||
|
return res;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
//FUCK YOU YOU SON OF A LEMON PARTY
|
||||||
|
}
|
||||||
|
|
||||||
|
// DOMImplementationLS domImplLS = (DOMImplementationLS) document
|
||||||
|
// .getImplementation();
|
||||||
|
// LSSerializer serializer = domImplLS.createLSSerializer();
|
||||||
|
// String str = serializer.writeToString(result.getDocument());
|
||||||
|
// return str;
|
||||||
|
return result.getString("/api/upload/@result");
|
||||||
|
} catch (IOException e) {
|
||||||
|
return "Failure";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UploadImageTask(MWApi api, Context context) {
|
||||||
|
this.api = api;
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_share);
|
setContentView(R.layout.activity_share);
|
||||||
app = (CommonsApplication)this.getApplicationContext();
|
app = (CommonsApplication)this.getApplicationContext();
|
||||||
|
|
||||||
backgroundImageView = (ImageView)findViewById(R.id.backgroundImage);
|
backgroundImageView = (ImageView)findViewById(R.id.backgroundImage);
|
||||||
|
titleEdit = (EditText)findViewById(R.id.titleEdit);
|
||||||
|
descEdit = (EditText)findViewById(R.id.descEdit);
|
||||||
|
uploadButton = (Button)findViewById(R.id.uploadButton);
|
||||||
|
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
|
|
||||||
|
final Context that = this;
|
||||||
|
|
||||||
if(intent.getAction().equals(Intent.ACTION_SEND)) {
|
if(intent.getAction().equals(Intent.ACTION_SEND)) {
|
||||||
if(intent.getType().startsWith("image/")) {
|
if(intent.getType().startsWith("image/")) {
|
||||||
ImageLoaderTask loader = new ImageLoaderTask(backgroundImageView);
|
ImageLoaderTask loader = new ImageLoaderTask(backgroundImageView);
|
||||||
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
||||||
loader.execute(imageUri);
|
loader.execute(imageUri);
|
||||||
|
|
||||||
|
uploadButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
UploadImageTask uploadImage = new UploadImageTask(app.getApi(), that);
|
||||||
|
Log.d("Commons", "Starting upload, yo!");
|
||||||
|
uploadImage.execute(imageUri.toString(), titleEdit.getText().toString(), descEdit.getText().toString(), "Mobile Upload represent!");
|
||||||
|
try {
|
||||||
|
String result = uploadImage.get();
|
||||||
|
Log.d("Commons", "Result is " + result);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
if(!app.getApi().isLoggedIn) {
|
||||||
|
Intent loginIntent = new Intent(this, LoginActivity.class);
|
||||||
|
this.startActivity(loginIntent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
getMenuInflater().inflate(R.menu.activity_share, menu);
|
getMenuInflater().inflate(R.menu.activity_share, menu);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue