23
23
import java .net .MalformedURLException ;
24
24
import java .net .URL ;
25
25
import java .util .HashMap ;
26
+ import java .util .List ;
26
27
import java .util .Map ;
27
28
import java .util .Properties ;
28
- import java .util .Random ;
29
29
30
30
import org .apache .cordova .DroidGap ;
31
31
import org .ektorp .CouchDbConnector ;
32
+ import org .ektorp .CouchDbInstance ;
32
33
import org .ektorp .DbAccessException ;
34
+ import org .ektorp .ViewQuery ;
35
+ import org .ektorp .ViewResult ;
36
+ import org .ektorp .http .HttpClient ;
37
+ import org .ektorp .impl .StdCouchDbInstance ;
38
+ import org .ektorp .support .DesignDocument ;
39
+ import org .ektorp .support .SimpleViewGenerator ;
33
40
34
41
import android .accounts .Account ;
35
42
import android .accounts .AccountManager ;
36
- import android .app .Activity ;
37
43
import android .app .ProgressDialog ;
38
44
import android .content .Context ;
39
45
import android .content .Intent ;
40
- import android .content .SharedPreferences ;
41
46
import android .content .res .AssetManager ;
42
47
import android .content .res .Resources ;
43
48
import android .os .Bundle ;
44
49
import android .os .Handler ;
50
+ import android .os .Looper ;
45
51
import android .util .Log ;
46
52
import android .view .Gravity ;
47
53
import android .view .KeyEvent ;
58
64
import com .couchbase .syncpoint .SyncpointClient ;
59
65
import com .couchbase .syncpoint .impl .SyncpointClientImpl ;
60
66
import com .couchbase .syncpoint .model .SyncpointChannel ;
61
- import com .couchbase .touchdb .TDBody ;
67
+ import com .couchbase .syncpoint .model .SyncpointInstallation ;
68
+ import com .couchbase .syncpoint .model .SyncpointSession ;
62
69
import com .couchbase .touchdb .TDDatabase ;
63
70
import com .couchbase .touchdb .TDServer ;
64
71
import com .couchbase .touchdb .TDView ;
72
+ import com .couchbase .touchdb .TDViewMapBlock ;
73
+ import com .couchbase .touchdb .ektorp .TouchDBHttpClient ;
65
74
import com .couchbase .touchdb .javascript .TDJavaScriptViewCompiler ;
66
75
import com .couchbase .touchdb .listener .TDListener ;
67
- import com .couchbase .touchdb .replicator .TDReplicator ;
68
76
69
77
public class AndroidCouchbaseCallback extends DroidGap
70
78
{
@@ -152,7 +160,6 @@ public void onCreate(Bundle savedInstanceState)
152
160
System .err .println ("Failed to open microlog property file" );
153
161
}
154
162
155
-
156
163
try {
157
164
server = new TDServer (filesDir );
158
165
listener = new TDListener (server , 8888 );
@@ -237,6 +244,26 @@ public void onCreate(Bundle savedInstanceState)
237
244
// create the syncpoint client
238
245
try {
239
246
syncpoint = new SyncpointClientImpl (getApplicationContext (), masterServerUrl , Constants .syncpointAppId );
247
+ SyncpointChannel channel = syncpoint .getMyChannel (syncpointDefaultChannelName );
248
+ SyncpointInstallation inst = channel .getInstallation (getApplicationContext ());
249
+ if (inst != null ) {
250
+ CouchDbConnector localDatabase = inst .getLocalDatabase (getApplicationContext ());
251
+ String localDatabaseName = localDatabase .getDatabaseName ();
252
+ Log .v (TAG , "localDatabaseName: " + localDatabaseName );
253
+ TDDatabase origDb = server .getDatabaseNamed (appDb );
254
+ origDb .open ();
255
+ TDDatabase newDb = server .getDatabaseNamed (localDatabaseName );
256
+ newDb .open ();
257
+ List <TDView > views = origDb .getAllViews ();
258
+ for (TDView tdView : views ) {
259
+ //TDView origView = origDb.getExistingViewNamed("aview");
260
+ TDViewMapBlock mapBlock = tdView .getMapBlock ();
261
+ TDView newView = newDb .getViewNamed ("aview" );
262
+ newView .setMapReduceBlocks (mapBlock , null , "1" );
263
+ }
264
+ origDb .close ();
265
+ newDb .close ();
266
+ }
240
267
} catch (DbAccessException e1 ) {
241
268
Log .e ( TAG , "Error: " , e1 );
242
269
e1 .printStackTrace ();
@@ -259,10 +286,6 @@ public void onCreate(Bundle savedInstanceState)
259
286
// replPull.start();
260
287
// TDReplicator replPush = db.getReplicator(remote, true, true);
261
288
// replPush.start();
262
-
263
- // TODO: Check if the pairing has been approved.
264
- SyncpointChannel channel = syncpoint .getMyChannel (Constants .syncpointDefaultChannelName );
265
- CouchDbConnector database = channel .ensureLocalDatabase (getApplicationContext ());
266
289
} catch (Exception e ) {
267
290
// TODO Auto-generated catch block
268
291
e .printStackTrace ();
@@ -370,6 +393,14 @@ private void register() {
370
393
if (selectedAccount != null ) {
371
394
try {
372
395
syncpoint .pairSession ("console" , selectedAccount .name );
396
+ HttpClient httpClient = new TouchDBHttpClient (server );
397
+ CouchDbInstance localServer = new StdCouchDbInstance (httpClient );
398
+ //CouchDbConnector userDb = localServer.createConnector("_users", false);
399
+ CouchDbConnector localControlDatabase = localServer .createConnector (SyncpointClientImpl .LOCAL_CONTROL_DATABASE_NAME , false );
400
+ //PairingUser pairingUser = session.getPairingUser();
401
+ //PairingUser result = userDb.get(PairingUser.class, pairingUser.getId());
402
+ //waitForPairingToComplete(localServer, localControlDatabase);
403
+ pairingDidComplete (localServer );
373
404
} catch (DbAccessException e ) {
374
405
Log .e ( TAG , "Error: " , e );
375
406
Toast .makeText (this , "Error: Unable to connect to Syncpoint Server: " + e .getMessage (), Toast .LENGTH_LONG ).show ();
@@ -378,7 +409,6 @@ private void register() {
378
409
}
379
410
}
380
411
}
381
-
382
412
private void unregister () {
383
413
if ( registered ) {
384
414
Log .d ( TAG , "unregister()" );
@@ -387,6 +417,59 @@ private void unregister() {
387
417
}
388
418
}
389
419
420
+ void waitForPairingToComplete (final CouchDbInstance localServer , final CouchDbConnector localControlDatabase ) {
421
+ Log .v (TAG , "Waiting for pairing to complete..." );
422
+ Looper l = Looper .getMainLooper ();
423
+ Handler h = new Handler (l );
424
+ h .postDelayed (new Runnable () {
425
+
426
+ @ Override
427
+ public void run () {
428
+ String message = "Checking to see if account has been authorized." ;
429
+ Toast .makeText (getApplicationContext (), message , Toast .LENGTH_SHORT ).show ();
430
+ Log .v (TAG , message );
431
+ SyncpointSession session = SyncpointSession .sessionInDatabase (getApplicationContext (), localServer , localControlDatabase );
432
+ if (session .isPaired ()) {
433
+ pairingDidComplete (localServer );
434
+ } else {
435
+ message = "Authorization is in progress. " ;
436
+ Toast .makeText (getApplicationContext (), message , Toast .LENGTH_SHORT ).show ();
437
+ Log .v (TAG , message );
438
+ waitForPairingToComplete (localServer , localControlDatabase );
439
+ }
440
+ /*PairingUser user = remote.get(PairingUser.class, userDoc.getId());
441
+ if("paired".equals(user.getPairingState())) {
442
+ pairingDidComplete(localServer);
443
+ } else {
444
+ message = "Pairing state is stuck at " + user.getPairingState();
445
+ Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
446
+ Log.v(TAG, message);
447
+ waitForPairingToComplete(localServer, session, remote, user);
448
+ }*/
449
+ }
450
+ }, 3000 );
451
+ }
452
+
453
+ void pairingDidComplete (final CouchDbInstance localServer ) {
454
+ //CouchDbConnector localControlDatabase = localServer.createConnector(SyncpointClientImpl.LOCAL_CONTROL_DATABASE_NAME, false);
455
+ //SyncpointSession session = SyncpointSession.sessionInDatabase(getApplicationContext(), localServer, localControlDatabase);
456
+ //if(session != null) {
457
+ //if(session.isPaired()) {
458
+
459
+ String message = "Authorization Completed" ;
460
+ Toast .makeText (getApplicationContext (), message , Toast .LENGTH_LONG ).show ();
461
+ Log .v (TAG , message );
462
+ SyncpointChannel channel = syncpoint .getMyChannel (Constants .syncpointDefaultChannelName );
463
+ CouchDbConnector database = channel .ensureLocalDatabase (getApplicationContext ());
464
+ // create a sample document to verify that replication in the channel is working
465
+ Map <String ,Object > testObject = new HashMap <String ,Object >();
466
+ testObject .put ("key" , "value" );
467
+ // store the document
468
+ database .create (testObject );
469
+ //}
470
+ //}
471
+ }
472
+
390
473
// kudos: http://stackoverflow.com/questions/6058843/android-how-to-select-texts-from-webview
391
474
private void emulateShiftHeld (WebView view )
392
475
{
0 commit comments