Ejemplo n.º 1
0
  private void showNoteInPane(int position) {
    if (rightPane == null) return;

    if (position == -1) position = 0;

    title.setText("");
    content.setText("");

    // save index and top position

    int index = getListView().getFirstVisiblePosition();
    View v = getListView().getChildAt(0);
    int top = (v == null) ? 0 : v.getTop();

    updateNotesList(query, position);

    // restore

    getListView().setSelectionFromTop(index, top);

    if (position >= adapter.getCount()) position = 0;

    Cursor item = (Cursor) adapter.getItem(position);
    if (item == null || item.getCount() == 0) {
      TLog.d(TAG, "Index {0} not found in list", position);
      return;
    }
    TLog.d(TAG, "Getting note {0}", position);

    long noteId = item.getInt(item.getColumnIndexOrThrow(Note.ID));
    uri = Uri.parse(CONTENT_URI + "/" + noteId);

    note = NoteManager.getNote(this, uri);
    TLog.v(TAG, "Note guid: {0}", note.getGuid());

    if (note != null) {
      TLog.d(TAG, "note {0} found", position);
      noteContent =
          new NoteContentBuilder()
              .setCaller(noteContentHandler)
              .setInputSource(note.getXmlContent())
              .setTitle(note.getTitle())
              .build();
      lastIndex = position;
    } else {
      TLog.d(TAG, "The note {0} doesn't exist", uri);
      final boolean proposeShortcutRemoval;
      final boolean calledFromShortcut =
          getIntent().getBooleanExtra(CALLED_FROM_SHORTCUT_EXTRA, false);
      final String shortcutName = getIntent().getStringExtra(SHORTCUT_NAME);
      proposeShortcutRemoval = calledFromShortcut && uri != null && shortcutName != null;

      if (proposeShortcutRemoval) {
        dialogString = shortcutName;
        showDialog(DIALOG_NOT_FOUND_SHORTCUT);
      } else showDialog(DIALOG_NOT_FOUND);
    }
  }
Ejemplo n.º 2
0
  public void onResume() {

    // if the SyncService was stopped because Android killed it, we should not show the progress
    // dialog any more
    if (SyncManager.getInstance().getCurrentService().activity == null) {
      TLog.i(
          TAG,
          "Android killed the SyncService while in background. We will remove the dialog now.");
      removeDialog(DIALOG_SYNC);
    }

    super.onResume();
    Intent intent = this.getIntent();

    SyncService currentService = SyncManager.getInstance().getCurrentService();

    if (currentService.needsAuth() && intent != null) {
      Uri uri = intent.getData();

      if (uri != null && uri.getScheme().equals("tomdroid")) {
        TLog.i(TAG, "Got url : {0}", uri.toString());

        showDialog(DIALOG_AUTH_PROGRESS);

        Handler handler =
            new Handler() {

              @Override
              public void handleMessage(Message msg) {
                if (authProgressDialog != null) authProgressDialog.dismiss();
                if (msg.what == SyncService.AUTH_COMPLETE) startSyncing(true);
              }
            };

        ((ServiceAuth) currentService).remoteAuthComplete(uri, handler);
      }
    }

    SyncManager.setActivity(this);
    SyncManager.setHandler(this.syncMessageHandler);

    // tablet refresh
    if (rightPane != null) {
      updateTextAttributes();
      if (!creating) showNoteInPane(lastIndex);
    } else updateNotesList(query, lastIndex);

    // set the view shown when the list is empty
    updateEmptyList(query);
    creating = false;
  }
Ejemplo n.º 3
0
  public void finishSync() {
    TLog.v(TAG, "Finishing Sync");

    removeDialog(DIALOG_SYNC);

    if (rightPane != null) showNoteInPane(lastIndex);
  }
Ejemplo n.º 4
0
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    TLog.d(TAG, "onActivityResult called with result {0}", resultCode);

    // returning from file picker
    if (data != null && data.hasExtra(FilePickerActivity.EXTRA_FILE_PATH)) {
      // Get the file path
      File f = new File(data.getStringExtra(FilePickerActivity.EXTRA_FILE_PATH));
      Uri noteUri = Uri.fromFile(f);
      Intent intent = new Intent(this, Receive.class);
      intent.setData(noteUri);
      startActivity(intent);
    } else { // returning from sync conflict
      SyncService currentService = SyncManager.getInstance().getCurrentService();
      currentService.resolvedConflict(requestCode);
    }
  }
Ejemplo n.º 5
0
  @SuppressWarnings("deprecation")
  private void startSyncing(boolean push) {

    String serverUri = Preferences.getString(Preferences.Key.SYNC_SERVER);
    SyncService currentService = SyncManager.getInstance().getCurrentService();

    if (currentService.needsAuth()) {

      // service needs authentication
      TLog.i(TAG, "Creating dialog");

      showDialog(DIALOG_AUTH_PROGRESS);

      Handler handler =
          new Handler() {

            @Override
            public void handleMessage(Message msg) {

              Uri authorizationUri = (Uri) msg.obj;
              if (authorizationUri != null) {

                resetSyncValues();

                Intent i = new Intent(Intent.ACTION_VIEW, authorizationUri);
                startActivity(i);

              } else {
                // Auth failed, don't update the value
                showDialog(DIALOG_CONNECT_FAILED);
              }

              if (authProgressDialog != null) authProgressDialog.dismiss();
            }
          };

      ((ServiceAuth) currentService).getAuthUri(serverUri, handler);
    } else {
      syncProcessedNotes = 0;
      syncTotalNotes = 0;
      dialogString = getString(R.string.syncing_connect);
      showDialog(DIALOG_SYNC);
      SyncManager.getInstance().startSynchronization(push); // push by default
    }
  }
Ejemplo n.º 6
0
  /** Called when the activity is created. */
  @SuppressLint("NewApi")
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Preferences.init(this, CLEAR_PREFERENCES);
    context = this;
    SyncManager.setActivity(this);
    SyncManager.setHandler(this.syncMessageHandler);

    main = View.inflate(this, R.layout.main, null);

    setContentView(main);

    // get the Path to the notes-folder from Preferences
    if (Preferences.getString(Preferences.Key.SD_LOCATION).startsWith("/")) {
      NOTES_PATH = Preferences.getString(Preferences.Key.SD_LOCATION);
    } else {
      NOTES_PATH =
          Environment.getExternalStorageDirectory()
              + "/"
              + Preferences.getString(Preferences.Key.SD_LOCATION)
              + "/";
    }

    // generate the http header we want to send on syncing
    getPackageManager();
    try {
      PackageInfo pi =
          getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
      HTTP_HEADER =
          String.format(
              "%1$s v%2$s, build %3$s, Android v%4$s, %5$s/%6$s",
              pi.packageName,
              pi.versionName,
              pi.versionCode,
              Build.VERSION.RELEASE,
              Build.MANUFACTURER,
              Build.MODEL);
    } catch (NameNotFoundException e) {
      e.printStackTrace();
      HTTP_HEADER = "Tomdroid vunknown, build unknown, Android unknown, unknown/unknown";
    }
    TLog.v(TAG, "Generated http-header: {0}: {1}", "X-Tomboy-Client", Tomdroid.HTTP_HEADER);

    // did we already show the warning and got destroyed by android's activity killer?
    if (Preferences.getBoolean(Preferences.Key.FIRST_RUN)) {
      TLog.i(TAG, "Tomdroid is first run.");

      // add a first explanatory note
      NoteManager.putNote(this, FirstNote.createFirstNote(this));

      // Warn that this is a "will eat your babies" release
      showDialog(DIALOG_FIRST_RUN);
    }

    this.intent = getIntent();

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      this.setTitle(getString(R.string.app_name) + " - " + getString(R.string.SearchResultTitle));
      query = intent.getStringExtra(SearchManager.QUERY);

      // adds query to search history suggestions
      SearchRecentSuggestions suggestions =
          new SearchRecentSuggestions(
              this, SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);
      suggestions.saveRecentQuery(query, null);
    } else {
      // if main view -> disable the tomdroid icon home button
      setHomeButtonEnabled(false);
    }

    String defaultSortOrder = Preferences.getString(Preferences.Key.SORT_ORDER);
    NoteManager.setSortOrder(defaultSortOrder);

    // set list adapter
    updateNotesList(query, -1);

    // add note to pane for tablet
    rightPane = (LinearLayout) findViewById(R.id.right_pane);
    registerForContextMenu(findViewById(android.R.id.list));

    // check if receiving note
    if (getIntent().hasExtra("view_note")) {
      uri = getIntent().getData();
      getIntent().setData(null);
      Intent i = new Intent(Intent.ACTION_VIEW, uri, this, ViewNote.class);
      startActivity(i);
    }

    if (rightPane != null) {
      content = (TextView) findViewById(R.id.content);
      title = (TextView) findViewById(R.id.title);

      // this we will call on resume as well.
      updateTextAttributes();
      showNoteInPane(0);
    }

    // set the view shown when the list is empty
    updateEmptyList(query);

    // Syncing if SyncOnStart (pref) set AND onCreate_SyncOnStart set false for syncing only on
    // startup
    if (Preferences.getBoolean(Preferences.Key.SYNC_ON_START) && first_onCreate_run) {
      startSyncing(true);
      TLog.i(TAG, "SyncOnStart activated");
    }

    // we already run onCreate now!
    first_onCreate_run = false;
  }
Ejemplo n.º 7
0
    @Override
    public void handleMessage(Message msg) {

      SyncService currentService = SyncManager.getInstance().getCurrentService();
      String serviceDescription = currentService.getDescription();
      String message = "";
      boolean dismiss = false;

      switch (msg.what) {
        case SyncService.AUTH_COMPLETE:
          message = getString(R.string.messageAuthComplete);
          message = String.format(message, serviceDescription);
          Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
          break;
        case SyncService.AUTH_FAILED:
          dismiss = true;
          message = getString(R.string.messageAuthFailed);
          message = String.format(message, serviceDescription);
          Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
          break;
        case SyncService.PARSING_COMPLETE:
          final ErrorList errors = (ErrorList) msg.obj;
          if (errors == null || errors.isEmpty()) {
            message = getString(R.string.messageSyncComplete);
            message = String.format(message, serviceDescription);
            Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
            finishSync();
          } else {
            TLog.v(TAG, "syncErrors: {0}", TextUtils.join("\n", errors.toArray()));
            dialogString = getString(R.string.messageSyncError);
            dialogBoolean = errors.save();
            showDialog(DIALOG_SYNC_ERRORS);
          }
          break;
        case SyncService.CONNECTING_FAILED:
          dismiss = true;
          message = getString(R.string.messageSyncConnectingFailed);
          message = String.format(message, serviceDescription);
          Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
          break;
        case SyncService.PARSING_FAILED:
          dismiss = true;
          message = getString(R.string.messageSyncParseFailed);
          message = String.format(message, serviceDescription);
          Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
          break;
        case SyncService.PARSING_NO_NOTES:
          dismiss = true;
          message = getString(R.string.messageSyncNoNote);
          message = String.format(message, serviceDescription);
          Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
          break;

        case SyncService.NO_INTERNET:
          dismiss = true;
          Toast.makeText(activity, getString(R.string.messageSyncNoConnection), Toast.LENGTH_SHORT)
              .show();
          break;

        case SyncService.NO_SD_CARD:
          dismiss = true;
          Toast.makeText(activity, activity.getString(R.string.messageNoSDCard), Toast.LENGTH_SHORT)
              .show();
          break;
        case SyncService.SYNC_CONNECTED:
          dialogString = getString(R.string.gettings_notes);
          showDialog(DIALOG_SYNC);
          break;
        case SyncService.BEGIN_PROGRESS:
          syncTotalNotes = msg.arg1;
          syncProcessedNotes = 0;
          dialogString = getString(R.string.syncing_local);
          showDialog(DIALOG_SYNC);
          break;
        case SyncService.SYNC_PROGRESS:
          if (msg.arg1 == 90) {
            dialogString = getString(R.string.syncing_remote);
            showDialog(DIALOG_SYNC);
          }
          break;
        case SyncService.NOTE_DELETED:
          message = getString(R.string.messageSyncNoteDeleted);
          message = String.format(message, serviceDescription);
          // Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
          break;

        case SyncService.NOTE_PUSHED:
          message = getString(R.string.messageSyncNotePushed);
          message = String.format(message, serviceDescription);
          // Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();

          break;
        case SyncService.NOTE_PULLED:
          message = getString(R.string.messageSyncNotePulled);
          message = String.format(message, serviceDescription);
          // Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
          break;

        case SyncService.NOTE_DELETE_ERROR:
          dismiss = true;
          message = getString(R.string.messageSyncNoteDeleteError);
          message = String.format(message, serviceDescription);
          Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
          break;

        case SyncService.NOTE_PUSH_ERROR:
          dismiss = true;
          message = getString(R.string.messageSyncNotePushError);
          message = String.format(message, serviceDescription);
          Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
          break;
        case SyncService.NOTE_PULL_ERROR:
          dismiss = true;
          message = getString(R.string.messageSyncNotePullError);
          message = String.format(message, serviceDescription);
          Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
          break;
        case SyncService.IN_PROGRESS:
          Toast.makeText(
                  activity,
                  activity.getString(R.string.messageSyncAlreadyInProgress),
                  Toast.LENGTH_SHORT)
              .show();
          dismiss = true;
          break;
        case SyncService.NOTES_BACKED_UP:
          Toast.makeText(
                  activity, activity.getString(R.string.messageNotesBackedUp), Toast.LENGTH_SHORT)
              .show();
          break;
        case SyncService.SYNC_CANCELLED:
          dismiss = true;
          message = getString(R.string.messageSyncCancelled);
          message = String.format(message, serviceDescription);
          Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
          break;
        default:
          break;
      }
      if (dismiss) removeDialog(DIALOG_SYNC);
    }