1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 19:42:17 +02:00

select account and authenticate in preferences activity

This commit is contained in:
Nikolay Pultsin 2014-08-02 06:18:25 +01:00
parent cfe5420c19
commit 7b8f7141d6
7 changed files with 45 additions and 21 deletions

View file

@ -27,8 +27,7 @@ import android.content.Context;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import org.geometerplus.zlibrary.core.network.ZLNetworkContext;
import org.geometerplus.zlibrary.core.network.ZLNetworkRequest;
import org.geometerplus.zlibrary.core.network.*;
public abstract class AndroidNetworkContext extends ZLNetworkContext {
@Override

View file

@ -28,6 +28,8 @@ import android.view.KeyEvent;
import org.geometerplus.zlibrary.core.application.ZLKeyBindings;
import org.geometerplus.zlibrary.core.language.Language;
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.zlibrary.core.network.JsonRequest;
import org.geometerplus.zlibrary.core.options.*;
import org.geometerplus.zlibrary.core.resources.ZLResource;
@ -45,12 +47,14 @@ import org.geometerplus.fbreader.tips.TipsManager;
import org.geometerplus.android.fbreader.DictionaryUtil;
import org.geometerplus.android.fbreader.FBReader;
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
import org.geometerplus.android.fbreader.network.auth.ActivityNetworkContext;
import org.geometerplus.android.fbreader.preferences.fileChooser.FileChooserCollection;
import org.geometerplus.android.util.DeviceType;
public class PreferenceActivity extends ZLPreferenceActivity {
private final FileChooserCollection myChooserCollection = new FileChooserCollection(this);
private final ActivityNetworkContext myNetworkContext = new ActivityNetworkContext(this);
private final FileChooserCollection myChooserCollection = new FileChooserCollection(this, 2000);
public PreferenceActivity() {
super("Preferences");
@ -58,6 +62,10 @@ public class PreferenceActivity extends ZLPreferenceActivity {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (myNetworkContext.onActivityResult(requestCode, resultCode, data)) {
return;
}
if (resultCode == RESULT_OK) {
myChooserCollection.update(requestCode, data);
}
@ -134,6 +142,24 @@ public class PreferenceActivity extends ZLPreferenceActivity {
) {
@Override
protected void onClick() {
if (isChecked()) {
new Thread() {
public void run() {
try {
myNetworkContext.perform(
new JsonRequest("https://demo.fbreader.org/login/test") {
@Override
public void processResponse(Object response) {
// TODO: update message
}
}
);
} catch (ZLNetworkException e) {
e.printStackTrace();
}
}
}.start();
}
super.onClick();
syncPreferences.run();
}

View file

@ -31,15 +31,17 @@ import org.geometerplus.zlibrary.core.resources.ZLResource;
public class FileChooserCollection {
private final Context myContext;
private final int myBaseRequestCode;
private final List<FileChooserPreference> myPreferences = new ArrayList<FileChooserPreference>();
public FileChooserCollection(Context context) {
public FileChooserCollection(Context context, int baseRequestCode) {
myContext = context;
myBaseRequestCode = baseRequestCode;
}
public FileChooserPreference createPreference(ZLResource rootResource, String resourceKey, ZLStringListOption option, Runnable onValueSetAction) {
final FileChooserPreference preference = new FileChooserStringListPreference(
myContext, rootResource, resourceKey, option, myPreferences.size(), onValueSetAction
myContext, rootResource, resourceKey, option, myBaseRequestCode + myPreferences.size(), onValueSetAction
);
myPreferences.add(preference);
return preference;
@ -47,15 +49,15 @@ public class FileChooserCollection {
public FileChooserPreference createPreference(ZLResource rootResource, String resourceKey, ZLStringOption option, Runnable onValueSetAction) {
final FileChooserPreference preference = new FileChooserStringPreference(
myContext, rootResource, resourceKey, option, myPreferences.size(), onValueSetAction
myContext, rootResource, resourceKey, option, myBaseRequestCode + myPreferences.size(), onValueSetAction
);
myPreferences.add(preference);
return preference;
}
public void update(int index, Intent data) {
public void update(int requestCode, Intent data) {
try {
myPreferences.get(index).setValueFromIntent(data);
myPreferences.get(requestCode - myBaseRequestCode).setValueFromIntent(data);
} catch (Exception e) {
// ignore
}

View file

@ -26,15 +26,15 @@ import android.preference.Preference;
import org.geometerplus.zlibrary.core.resources.ZLResource;
abstract class FileChooserPreference extends Preference {
protected final int myRegCode;
protected final int myRequestCode;
protected final ZLResource myResource;
protected final boolean myChooseWritableDirectoriesOnly;
protected final Runnable myOnValueSetAction;
FileChooserPreference(Context context, ZLResource rootResource, String resourceKey, boolean chooseWritableDirectoriesOnly, int regCode, Runnable onValueSetAction) {
FileChooserPreference(Context context, ZLResource rootResource, String resourceKey, boolean chooseWritableDirectoriesOnly, int requestCode, Runnable onValueSetAction) {
super(context);
myRegCode = regCode;
myRequestCode = requestCode;
myResource = rootResource.getResource(resourceKey);
setTitle(myResource.getValue());

View file

@ -34,8 +34,8 @@ import org.geometerplus.android.util.FileChooserUtil;
class FileChooserStringListPreference extends FileChooserPreference {
private final ZLStringListOption myOption;
FileChooserStringListPreference(Context context, ZLResource rootResource, String resourceKey, ZLStringListOption option, int regCode, Runnable onValueSetAction) {
super(context, rootResource, resourceKey, false, regCode, onValueSetAction);
FileChooserStringListPreference(Context context, ZLResource rootResource, String resourceKey, ZLStringListOption option, int requestCode, Runnable onValueSetAction) {
super(context, rootResource, resourceKey, false, requestCode, onValueSetAction);
myOption = option;
@ -46,7 +46,7 @@ class FileChooserStringListPreference extends FileChooserPreference {
protected void onClick() {
FileChooserUtil.runFolderListDialog(
(Activity)getContext(),
myRegCode,
myRequestCode,
myResource.getValue(),
myResource.getResource("chooserTitle").getValue(),
myOption.getValue(),

View file

@ -32,8 +32,8 @@ import org.geometerplus.android.util.FileChooserUtil;
class FileChooserStringPreference extends FileChooserPreference {
private final ZLStringOption myOption;
FileChooserStringPreference(Context context, ZLResource rootResource, String resourceKey, ZLStringOption option, int regCode, Runnable onValueSetAction) {
super(context, rootResource, resourceKey, true, regCode, onValueSetAction);
FileChooserStringPreference(Context context, ZLResource rootResource, String resourceKey, ZLStringOption option, int requestCode, Runnable onValueSetAction) {
super(context, rootResource, resourceKey, true, requestCode, onValueSetAction);
myOption = option;
setSummary(getStringValue());
@ -43,7 +43,7 @@ class FileChooserStringPreference extends FileChooserPreference {
protected void onClick() {
FileChooserUtil.runDirectoryChooser(
(Activity)getContext(),
myRegCode,
myRequestCode,
myResource.getResource("chooserTitle").getValue(),
getStringValue(),
myChooseWritableDirectoriesOnly

View file

@ -17,15 +17,12 @@
* 02110-1301, USA.
*/
package org.geometerplus.android.fbreader.network.auth;
package org.geometerplus.zlibrary.core.network;
import java.io.*;
import org.json.simple.JSONValue;
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.zlibrary.core.network.ZLNetworkRequest;
public abstract class JsonRequest extends ZLNetworkRequest.PostWithMap {
public JsonRequest(String url) {
super(url);