mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 02:39:23 +02:00
AndroidNetworkContext (base class for ActivityNetworkContext & ServiceNetworkContext) + cookies in SynchroniserService
This commit is contained in:
parent
9e961449d1
commit
9bee7c323d
5 changed files with 86 additions and 27 deletions
|
@ -26,6 +26,7 @@ import java.util.*;
|
||||||
|
|
||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
@ -40,7 +41,19 @@ import org.apache.http.impl.cookie.BasicClientCookie2;
|
||||||
import org.geometerplus.zlibrary.core.network.*;
|
import org.geometerplus.zlibrary.core.network.*;
|
||||||
import org.geometerplus.android.fbreader.OrientationUtil;
|
import org.geometerplus.android.fbreader.OrientationUtil;
|
||||||
|
|
||||||
public final class ActivityNetworkContext extends ZLNetworkContext {
|
public final class ActivityNetworkContext extends AndroidNetworkContext {
|
||||||
|
private final Activity myActivity;
|
||||||
|
private volatile String myAccount;
|
||||||
|
private volatile boolean myAuthorizationConfirmed;
|
||||||
|
|
||||||
|
public ActivityNetworkContext(Activity activity) {
|
||||||
|
myActivity = activity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Context getContext() {
|
||||||
|
return myActivity;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
|
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
boolean processed = true;
|
boolean processed = true;
|
||||||
try {
|
try {
|
||||||
|
@ -90,26 +103,6 @@ public final class ActivityNetworkContext extends ZLNetworkContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Activity myActivity;
|
|
||||||
private volatile String myAccount;
|
|
||||||
private volatile boolean myAuthorizationConfirmed;
|
|
||||||
|
|
||||||
public ActivityNetworkContext(Activity activity) {
|
|
||||||
myActivity = activity;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean authenticate(URI uri, Map<String,String> params) {
|
|
||||||
System.err.println("AUTHENTICATE FOR " + uri);
|
|
||||||
if (!"https".equalsIgnoreCase(uri.getScheme())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return GooglePlayServicesUtil.isGooglePlayServicesAvailable(myActivity)
|
|
||||||
== ConnectionResult.SUCCESS
|
|
||||||
? authenticateToken(uri, params)
|
|
||||||
: authenticateWeb(uri, params);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String url(URI base, Map<String,String> params, String key) {
|
private String url(URI base, Map<String,String> params, String key) {
|
||||||
final String path = params.get(key);
|
final String path = params.get(key);
|
||||||
if (path == null) {
|
if (path == null) {
|
||||||
|
@ -123,7 +116,8 @@ public final class ActivityNetworkContext extends ZLNetworkContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean authenticateWeb(URI uri, Map<String,String> params) {
|
@Override
|
||||||
|
protected boolean authenticateWeb(URI uri, Map<String,String> params) {
|
||||||
System.err.println("+++ WEB AUTH +++");
|
System.err.println("+++ WEB AUTH +++");
|
||||||
final String authUrl = url(uri, params, "auth-url-web");
|
final String authUrl = url(uri, params, "auth-url-web");
|
||||||
final String completeUrl = url(uri, params, "complete-url-web");
|
final String completeUrl = url(uri, params, "complete-url-web");
|
||||||
|
@ -173,7 +167,8 @@ public final class ActivityNetworkContext extends ZLNetworkContext {
|
||||||
return buffer.toString().trim();
|
return buffer.toString().trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean authenticateToken(URI uri, Map<String,String> params) {
|
@Override
|
||||||
|
protected boolean authenticateToken(URI uri, Map<String,String> params) {
|
||||||
System.err.println("+++ TOKEN AUTH +++");
|
System.err.println("+++ TOKEN AUTH +++");
|
||||||
try {
|
try {
|
||||||
final String authUrl = url(uri, params, "auth-url-token");
|
final String authUrl = url(uri, params, "auth-url-token");
|
||||||
|
|
|
@ -33,6 +33,7 @@ import org.geometerplus.zlibrary.core.network.ZLNetworkException;
|
||||||
import org.geometerplus.zlibrary.core.util.MimeType;
|
import org.geometerplus.zlibrary.core.util.MimeType;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.ui.android.R;
|
import org.geometerplus.zlibrary.ui.android.R;
|
||||||
|
import org.geometerplus.zlibrary.ui.android.network.SQLiteCookieDatabase;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.network.*;
|
import org.geometerplus.fbreader.network.*;
|
||||||
import org.geometerplus.fbreader.network.opds.OPDSCustomNetworkLink;
|
import org.geometerplus.fbreader.network.opds.OPDSCustomNetworkLink;
|
||||||
|
@ -56,6 +57,7 @@ public class AddCustomCatalogActivity extends Activity {
|
||||||
super.onCreate(icicle);
|
super.onCreate(icicle);
|
||||||
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
|
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
|
||||||
|
|
||||||
|
SQLiteCookieDatabase.init(this);
|
||||||
AuthenticationActivity.initCredentialsCreator(this);
|
AuthenticationActivity.initCredentialsCreator(this);
|
||||||
|
|
||||||
setContentView(R.layout.add_custom_catalog);
|
setContentView(R.layout.add_custom_catalog);
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2010-2014 Geometer Plus <contact@geometerplus.com>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||||
|
* 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.geometerplus.android.fbreader.network;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public abstract class AndroidNetworkContext extends ZLNetworkContext {
|
||||||
|
@Override
|
||||||
|
public boolean authenticate(URI uri, Map<String,String> params) {
|
||||||
|
System.err.println("REQUESTED AUTH FOR " + uri);
|
||||||
|
for (Object o : cookieStore().getCookies()) {
|
||||||
|
System.err.println("AUTH COOKIE: " + o);
|
||||||
|
}
|
||||||
|
if (!"https".equalsIgnoreCase(uri.getScheme())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return GooglePlayServicesUtil.isGooglePlayServicesAvailable(getContext())
|
||||||
|
== ConnectionResult.SUCCESS
|
||||||
|
? authenticateToken(uri, params)
|
||||||
|
: authenticateWeb(uri, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract Context getContext();
|
||||||
|
protected abstract boolean authenticateWeb(URI uri, Map<String,String> params);
|
||||||
|
protected abstract boolean authenticateToken(URI uri, Map<String,String> params);
|
||||||
|
}
|
|
@ -23,18 +23,27 @@ import java.net.URI;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.network.ZLNetworkContext;
|
public final class ServiceNetworkContext extends AndroidNetworkContext {
|
||||||
|
|
||||||
public final class ServiceNetworkContext extends ZLNetworkContext {
|
|
||||||
private final Service myService;
|
private final Service myService;
|
||||||
|
|
||||||
public ServiceNetworkContext(Service service) {
|
public ServiceNetworkContext(Service service) {
|
||||||
myService = service;
|
myService = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Context getContext() {
|
||||||
|
return myService;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean authenticate(URI uri, Map<String,String> params) {
|
protected boolean authenticateWeb(URI uri, Map<String,String> params) {
|
||||||
|
// TODO: implement
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean authenticateToken(URI uri, Map<String,String> params) {
|
||||||
// TODO: implement
|
// TODO: implement
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ import android.os.IBinder;
|
||||||
import org.json.simple.JSONValue;
|
import org.json.simple.JSONValue;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.network.*;
|
import org.geometerplus.zlibrary.core.network.*;
|
||||||
|
import org.geometerplus.zlibrary.ui.android.network.SQLiteCookieDatabase;
|
||||||
import org.geometerplus.fbreader.book.*;
|
import org.geometerplus.fbreader.book.*;
|
||||||
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
|
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
|
||||||
import org.geometerplus.android.fbreader.network.ServiceNetworkContext;
|
import org.geometerplus.android.fbreader.network.ServiceNetworkContext;
|
||||||
|
@ -43,6 +44,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
|
SQLiteCookieDatabase.init(this);
|
||||||
myCollection.bindToService(this, this);
|
myCollection.bindToService(this, this);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue