1 18 19 package sync4j.syncclient.blackberry; 20 21 import java.util.Hashtable ; 22 23 import net.rim.blackberry.api.pdap.BlackBerryPIMList; 24 import javax.microedition.pim.PIM; 25 26 import net.rim.device.api.system.Bitmap; 27 import net.rim.device.api.system.PersistentObject; 28 import net.rim.device.api.system.PersistentStore; 29 import net.rim.device.api.system.TrackwheelListener; 30 31 import net.rim.device.api.ui.Field; 32 import net.rim.device.api.ui.MenuItem; 33 import net.rim.device.api.ui.UiApplication; 34 35 import net.rim.device.api.ui.component.BasicEditField; 36 import net.rim.device.api.ui.component.BitmapField; 37 import net.rim.device.api.ui.component.CheckboxField; 38 import net.rim.device.api.ui.component.Dialog; 39 import net.rim.device.api.ui.component.EditField; 40 import net.rim.device.api.ui.component.LabelField; 41 import net.rim.device.api.ui.component.Menu; 42 import net.rim.device.api.ui.component.PasswordEditField; 43 import net.rim.device.api.ui.component.RichTextField; 44 import net.rim.device.api.ui.component.SeparatorField; 45 import net.rim.device.api.ui.container.MainScreen; 46 47 import sync4j.syncclient.blackberry.listener.Sync4jContactListener; 48 import sync4j.syncclient.blackberry.listener.Sync4jEventListener; 49 50 import sync4j.syncclient.spds.SyncManagerFactory; 51 import sync4j.syncclient.util.StaticDataHelper; 52 53 61 62 public class SyncClient 63 extends UiApplication 64 implements Runnable , TrackwheelListener{ 65 66 68 final private static String PROP_SERVER_URL = "serverUrl" ; 69 final private static String PROP_LOGIN = "login" ; 70 final private static String PROP_GATEWAYAPN = "gatewayApn" ; 71 final private static String PROP_GATEWAYIP = "gatewayIp" ; 72 final private static String PROP_GATEWAYPORT = "gatewayPort" ; 73 74 final private static String ERROR_NOT_AUTHORIZED = "401" ; 75 final private static String ERROR_NOT_FOUND = "404" ; 76 77 final private static String IMMAGE_LOGO = "arrows_sync.gif" ; 78 79 final private static String CONTACT_SORCE_TYPE = "text/x-s4j-contact" ; 80 final private static String CONTACT_SOURCE_NAME = "xcard" ; 81 final private static String CALENDAR_SOURCE_TYPE = "text/x-s4j-calendar" ; 82 final private static String CALENDAR_SOURCE_NAME = "xcal" ; 83 84 86 private static PersistentObject store; 87 88 private static long CONFIG_KEY = 0xb565a75eda53298eL ; 89 90 92 private Thread thread = null ; 93 private AppScreen appScreen = null ; 94 private RichTextField statusField = null ; 95 private BlackBerryPIMList listContact = null ; 96 private BlackBerryPIMList listEvent = null ; 97 private Sync4jContactListener listenerContact = null ; 98 private Sync4jEventListener listenerEvent = null ; 99 100 private Hashtable properties = null ; 101 102 private StaticDataHelper sdh = null ; 103 104 private String contactSourceType = null ; 105 private String contactSourceName = null ; 106 107 private String calendarSourceType = null ; 108 private String calendarSourceName = null ; 109 110 static { 111 store = PersistentStore.getPersistentObject(CONFIG_KEY); 112 } 113 114 119 public SyncClient() 120 throws Exception { 121 122 sdh = new StaticDataHelper(); 126 sdh.loadLanguage(this.getClass()); 127 128 sdh.loadDefaultValue(); 132 133 listContact = (BlackBerryPIMList) 134 PIM.getInstance(). 135 openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); 136 137 listEvent = (BlackBerryPIMList) 138 PIM.getInstance(). 139 openPIMList(PIM.EVENT_LIST , PIM.READ_WRITE); 140 141 listenerContact = new Sync4jContactListener (); 142 listenerEvent = new Sync4jEventListener (); 143 144 statusField = new RichTextField("",Field.NON_FOCUSABLE) ; 145 appScreen = new AppScreen() ; 146 147 listContact.addListener(listenerContact) ; 148 listEvent.addListener(listenerEvent) ; 149 150 synchronized(store) { 151 152 String [] contents = (String [])store.getContents(); 153 154 if(contents == null) { 155 156 displayStatus(sdh.getLanguage("conf-not-set")); 157 158 } else { 159 160 String syncContact = contents[3] ; 161 String syncCalendar = contents[4] ; 162 163 if (!("true".equals(syncContact)) && !("true".equals(syncCalendar))) { 164 displayStatus(sdh.getLanguage("nothing-sync-enable")); 165 } 166 } 167 168 } 169 170 synchronized(getEventLock()) { 171 pushScreen(appScreen); 172 } 173 } 174 175 182 public void synchronize() 183 throws Exception { 184 try { 185 186 String [] contents = null; 187 188 contactSourceType = CONTACT_SORCE_TYPE ; 189 contactSourceName = CONTACT_SOURCE_NAME ; 190 calendarSourceType = CALENDAR_SOURCE_TYPE ; 191 calendarSourceName = CALENDAR_SOURCE_NAME ; 192 193 synchronized(store) { 194 contents = (String [])store.getContents(); 195 if(contents == null) { 196 displayStatus(sdh.getLanguage("conf-not-set")); 197 return; 198 } 199 } 200 201 final String serverURL = contents[ 0 ] ; 202 final String contactSourceURI = contents[ 1 ] ; 203 final String calendarSourceURI = contents[ 2 ] ; 204 final String syncContact = contents[ 3 ] ; 205 final String syncCalendar = contents[ 4 ] ; 206 final String gatewayApn = contents[ 7 ] ; 207 final String gatewayIp = contents[ 8 ] ; 208 final String gatewayPort = contents[ 9 ] ; 209 210 final String login = contents[ 5 ] + 211 ":" + 212 contents[ 6 ] ; 213 214 if (!("true".equals(syncContact)) && !("true".equals(syncCalendar))) { 215 displayStatus(sdh.getLanguage("nothing-sync-enable") + 216 ":\n" + 217 sdh.getLanguage("please-configure") ); 218 return; 219 } 220 221 sdh.setCalendarSourceUri (calendarSourceURI ) ; 222 sdh.setContactSourceUri (contactSourceURI ) ; 223 224 properties = new Hashtable (); 225 226 properties.put(PROP_SERVER_URL , serverURL ); 227 properties.put(PROP_LOGIN , login ); 228 properties.put(PROP_GATEWAYAPN , gatewayApn ); 229 properties.put(PROP_GATEWAYIP , gatewayIp ); 230 properties.put(PROP_GATEWAYPORT , gatewayPort ); 231 232 if ("true".equals(syncContact) || "true".equals(syncCalendar)) { 233 234 Thread syncThread = new Thread (){ 235 public void run() { 236 237 if ("true".equals(syncContact)) { 238 displayStatus(sdh.getLanguage("synch-contact-wait") + 239 "\n" + 240 sdh.getLanguage("please-wait") ); 241 listContact.removeListener(listenerContact); 242 try { 243 244 SyncManagerFactory.getSyncManager(contactSourceURI, 245 contactSourceType, 246 contactSourceName, 247 properties).sync(); 248 listContact.addListener(listenerContact); 249 250 } catch(Exception e) { 251 displayStatus(sdh.getLanguage("synch-failed") + 252 " " + 253 getSynchError(e.getMessage() , 254 "address book" , 255 contactSourceURI) ); 256 return; 257 } 258 } 259 260 if ("true".equals(syncCalendar)) { 261 displayStatus(sdh.getLanguage("synch-calendar-wait") + 262 "\n" + 263 sdh.getLanguage("please-wait") ); 264 listEvent.removeListener(listenerEvent); 265 266 try { 267 268 SyncManagerFactory.getSyncManager(calendarSourceURI, 269 calendarSourceType, 270 calendarSourceName, 271 properties).sync(); 272 listEvent.addListener(listenerEvent); 273 274 } catch(Exception e) { 275 displayStatus(sdh.getLanguage("synch-failed") + 276 " " + 277 getSynchError(e.getMessage() , 278 "calendar" , 279 calendarSourceURI) ); 280 return; 281 } 282 } 283 284 displayStatus(sdh.getLanguage("synch-success")); 285 286 } 287 }; 288 289 syncThread.start(); 290 291 } 292 293 } catch(Exception e) { 294 displayStatus(sdh.getLanguage("synch-failed") + " " + e.getMessage()); 295 throw e; 296 } 297 } 298 299 305 void displayStatus(String message) { 306 synchronized(getEventLock()) { 307 308 AppScreen as = null; 309 310 statusField = new RichTextField("", Field.NON_FOCUSABLE) ; 311 as = new AppScreen() ; 312 statusField.setText(message); 313 as.setStatus(statusField); 314 pushScreen(as); 315 316 } 317 } 318 319 public boolean trackwheelClick(int status, int line) { 321 return true; 322 } 323 324 public boolean trackwheelUnclick(int status, int line) { 326 return true; 327 } 328 329 public boolean trackwheelRoll(int amount,int status, int line) { 331 return true; 332 } 333 334 341 public void run() { 342 try { 343 344 Thread mythread = Thread.currentThread(); 345 346 if (thread == mythread) { 347 348 synchronized (this) { 349 synchronize(); 350 } 351 } 352 353 } catch(Exception ex) { 354 displayStatus(sdh.getLanguage("exch-while-synch") + " " + ex); 355 } 356 } 357 358 364 private String getSynchError(String error , 365 String remoteDBType , 366 String sourceUri ){ 367 368 if (ERROR_NOT_AUTHORIZED.equals(error)) { 369 370 return sdh.getLanguage("not-authorized-user-and-password"); 371 372 } else if (ERROR_NOT_FOUND.equals(error)) { 373 374 return sdh.getLanguage("not-found-remote-syncsource") + 375 ": " + 376 sourceUri ; 377 378 } else if (error != null && error.indexOf("Connection refused") != -1) { 379 380 return sdh.getLanguage("error-connecting-to-the-server"); 381 382 } else if (error != null && error.indexOf("out-of-memory") != -1) { 383 384 return sdh.getLanguage("out-of-memory"); 385 386 } else { 387 388 return error; 389 390 } 391 392 } 393 394 398 class AppScreen extends MainScreen { 399 private ConfigurationScreen configScreen; 401 402 StaticDataHelper sdh = new StaticDataHelper() ; 403 404 408 public AppScreen() { 409 Bitmap icon = null ; 410 BitmapField iconField = null ; 411 412 icon = Bitmap.getBitmapResource(IMMAGE_LOGO); 413 iconField = new BitmapField(icon); 414 415 iconField.setSpace(76, 18); 416 417 configScreen = new ConfigurationScreen(); 418 419 setTitle (new LabelField(sdh.getLanguage("sync4j-bb-syncclient") ) ); 420 421 add (new LabelField(sdh.getLanguage("goto-menu-for-changing") ) ); 422 add (new LabelField(sdh.getLanguage("or-to-synchronize") ) ); 423 add (iconField); 424 425 } 426 427 431 private MenuItem configMenu = new MenuItem(sdh.getLanguage("configure"), 20000, 10) { 432 433 public void run() { 434 synchronized(getEventLock()) { 435 pushScreen(configScreen); 436 } 437 } 438 439 }; 440 441 446 private MenuItem SyncMenu = new MenuItem(sdh.getLanguage("synchronize"), 10000, 9) { 447 public void run() { 448 synchronized(this) { 449 thread = new Thread (SyncClient.this); 450 thread.start(); 451 } 452 } 453 }; 454 455 462 public void makeMenu(Menu menu, int instance) { 463 menu.add(SyncMenu ); 464 menu.add(configMenu ); 465 } 466 467 473 public boolean onClose() 474 { 475 int response = 0; 476 477 response = Dialog.ask(Dialog.D_YES_NO, sdh.getLanguage("do-you-want-to-exit")); 478 479 if (Dialog.YES == response ) { 480 System.exit(0); 481 } 482 if(Dialog.NO == response) { 483 return false; 484 } 485 486 return true; 487 } 488 } 489 490 494 class ConfigurationScreen extends MainScreen { 495 496 StaticDataHelper sdh = new StaticDataHelper() ; 497 498 private BasicEditField homePageField = null ; 500 private BasicEditField userNameField = null ; 501 private BasicEditField contactSourceUriField = null ; 502 private BasicEditField calendarSourceUriField = null ; 503 private PasswordEditField passwordField = null ; 504 private BasicEditField gatewayApnField = null ; 505 private BasicEditField gatewayIpField = null ; 506 private BasicEditField gatewayPortField = null ; 507 private CheckboxField syncContactField = null ; 508 private CheckboxField syncCalendarField = null ; 509 510 515 public ConfigurationScreen() { 516 517 setTitle(new LabelField(sdh.getLanguage("configure-sync4j-bb"))); 518 519 userNameField = new BasicEditField 520 (sdh.getLanguage("user") + " " , 521 sdh.getUserDefault() ); 522 523 passwordField = new PasswordEditField 524 (sdh.getLanguage("password") + " " , 525 sdh.getPasswordDefault() ); 526 527 homePageField = new BasicEditField 528 (sdh.getLanguage("homepage") + " \n" , 529 sdh.getHomePageDefault() ); 530 531 contactSourceUriField = new BasicEditField 532 (sdh.getLanguage("source-uri-contact") + " " , 533 sdh.getContactSourceUriDefault() ); 534 535 calendarSourceUriField = new BasicEditField 536 (sdh.getLanguage("source-uri-calendar") + " " , 537 sdh.getCalendarSourceUriDefault() ); 538 539 syncContactField = new CheckboxField 540 (sdh.getLanguage("enable-sync-contact") , 541 sdh.getSyncContactDefault() ); 542 543 syncCalendarField = new CheckboxField 544 (sdh.getLanguage("enable-sync-calendar") , 545 sdh.getSyncCalendarDefault() ); 546 547 gatewayApnField = new BasicEditField 548 (sdh.getLanguage("gateway-apn") + " " , 549 sdh.getGatewayApnDefault() ); 550 551 gatewayIpField = new BasicEditField 552 (sdh.getLanguage("gateway-ip") + " " , 553 sdh.getGatewayIpDefault() ); 554 555 gatewayPortField = new BasicEditField 556 (sdh.getLanguage("gateway-port") + " " , 557 sdh.getGatewayPortDefault() , 558 6 , 559 EditField.FILTER_INTEGER ); 560 561 SeparatorField separator1 = new SeparatorField(); 562 SeparatorField separator2 = new SeparatorField(); 563 SeparatorField separator3 = new SeparatorField(); 564 SeparatorField separator4 = new SeparatorField(); 565 566 add(userNameField ); 567 add(passwordField ); 568 add(separator1 ); 569 add(homePageField ); 570 add(contactSourceUriField ); 571 add(calendarSourceUriField ); 572 add(separator2 ); 573 add(syncContactField ); 574 add(syncCalendarField ); 575 add(separator3 ); 576 add(separator4 ); 577 add(gatewayApnField ); 578 add(gatewayIpField ); 579 add(gatewayPortField ); 580 581 populateData(); 582 } 583 584 589 private void populateData() { 590 String [] data = null; 591 592 synchronized(store) { 593 data = (String [])store.getContents(); 594 } 595 596 if(data != null) { 597 598 homePageField.setText (data[0]); 599 contactSourceUriField.setText (data[1]); 600 calendarSourceUriField.setText (data[2]); 601 602 if ("true".equals(data[3])) { 603 syncContactField.setChecked (true ); 604 } else { 605 syncContactField.setChecked (false ); 606 } 607 608 if ("true".equals(data[4])) { 609 syncCalendarField.setChecked (true ); 610 } else { 611 syncCalendarField.setChecked (false ); 612 } 613 614 userNameField.setText (data[5] ) ; 615 passwordField.setText (data[6] ) ; 616 gatewayApnField.setText (data[7] ) ; 617 gatewayIpField.setText (data[8] ) ; 618 gatewayPortField.setText (data[9] ) ; 619 620 } 621 } 622 623 627 private MenuItem saveConfItem = new MenuItem(sdh.getLanguage("save"), 20000, 10) { 628 629 public void run() { 630 saveConfiguration(); 631 } 632 633 }; 634 635 642 public void saveConfiguration(){ 643 644 String homePage = null ; 645 String userName = null ; 646 String password = null ; 647 String gatewayApn = null ; 648 String gatewayIp = null ; 649 String gatewayPort = null ; 650 String contactSourceUri = null ; 651 String calendarSourceUri = null ; 652 String syncContact = null ; 653 String syncCalendar = null ; 654 655 homePage = homePageField.getText ().trim(); 656 contactSourceUri = contactSourceUriField.getText ().trim(); 657 calendarSourceUri = calendarSourceUriField.getText ().trim(); 658 659 if (syncContactField.getChecked()) { 660 syncContact = "true"; 661 } else { 662 syncContact = "false"; 663 } 664 665 if (syncCalendarField.getChecked()) { 666 syncCalendar = "true"; 667 } else { 668 syncCalendar = "false"; 669 } 670 671 userName = userNameField.getText ().trim() ; 672 password = passwordField.getText ().trim() ; 673 gatewayApn = gatewayApnField.getText ().trim() ; 674 gatewayIp = gatewayIpField.getText ().trim() ; 675 gatewayPort = gatewayPortField.getText ().trim() ; 676 677 if (homePage == null || 678 contactSourceUri == null || 679 calendarSourceUri == null || 680 userName == null || 681 password == null) { 682 683 displayStatus(sdh.getLanguage("configuration-incomplete")); 684 685 } else if (homePage.equals ("") || 686 contactSourceUri.equals ("") || 687 calendarSourceUri.equals ("") || 688 userName.equals ("") || 689 password.equals ("")) { 690 691 displayStatus(sdh.getLanguage("configuration-incomplete")); 692 693 } else if ((homePage.indexOf("http") != 0) && (homePage.indexOf("https") != 0)) { 694 695 displayStatus(sdh.getLanguage("homepage-not-correct")); 696 697 } else { 698 699 String [] data = {homePage , 700 contactSourceUri , 701 calendarSourceUri , 702 syncContact , 703 syncCalendar , 704 userName , 705 password , 706 gatewayApn , 707 gatewayIp , 708 gatewayPort }; 709 710 synchronized(store) { 711 store.setContents(data); 712 store.commit(); 713 } 714 715 displayStatus(sdh.getLanguage("data-properly-configured")); 716 717 } 718 719 popScreen(ConfigurationScreen.this); 720 } 721 722 728 public void makeMenu(Menu menu, int instance) { 729 menu.add(saveConfItem); 730 } 731 732 739 public boolean onClose() { 740 741 int response = Dialog.ask(Dialog.D_SAVE,sdh.getLanguage("do-you-want-save")); 742 743 if (Dialog.SAVE == response ) { 744 saveConfiguration(); 745 } else if( Dialog.CANCEL == response) { 746 displayStatus(sdh.getLanguage("no-changes-to-previous")); 747 } else if ( Dialog.DISCARD == response ) { 748 popScreen(ConfigurationScreen.this); 749 displayStatus(sdh.getLanguage("no-changes-to-previous")); 750 } 751 752 return true; 753 } 754 } 755 756 761 public static void main(String s[]) throws Exception { 762 SyncClient test = new SyncClient(); 763 test.enterEventDispatcher(); 764 } 765 766 } 767 | Popular Tags |