Skip to content

Commit

Permalink
Support share content to Leanote
Browse files Browse the repository at this point in the history
  • Loading branch information
xingstarx committed May 21, 2018
1 parent 1e4c2cc commit d25558c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
8 changes: 7 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@
<activity
android:name=".ui.edit.NoteEditActivity"
android:screenOrientation="portrait"
android:label="@string/edit" />
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
<activity
android:name=".ui.NotePreviewActivity"
android:screenOrientation="portrait"
Expand Down
43 changes: 38 additions & 5 deletions app/src/main/java/org/houxg/leamonax/ui/edit/NoteEditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.view.ViewGroup;

import com.elvishew.xlog.XLog;
import com.tencent.bugly.crashreport.CrashReport;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
Expand All @@ -21,15 +22,20 @@
import org.houxg.leamonax.R;
import org.houxg.leamonax.ReadableException;
import org.houxg.leamonax.database.NoteDataStore;
import org.houxg.leamonax.database.NotebookDataStore;
import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.CompleteEvent;
import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.model.Tag;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.service.NoteFileService;
import org.houxg.leamonax.service.NoteService;
import org.houxg.leamonax.ui.BaseActivity;
import org.houxg.leamonax.utils.CollectionUtils;
import org.houxg.leamonax.utils.DialogDisplayer;
import org.houxg.leamonax.utils.NetworkUtils;
import org.houxg.leamonax.utils.ShareUtils;
import org.houxg.leamonax.utils.ToastUtils;
import org.houxg.leamonax.widget.LeaViewPager;

Expand Down Expand Up @@ -70,8 +76,9 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_note);
initToolBar((Toolbar) findViewById(R.id.toolbar), true);
mPager = (LeaViewPager) findViewById(R.id.pager);
Toolbar toolbar = findViewById(R.id.toolbar);
initToolBar(toolbar, true);
mPager = findViewById(R.id.pager);
mPager.setPagingEnabled(false);
mPager.setAdapter(new SectionAdapter(getSupportFragmentManager()));
mPager.setOffscreenPageLimit(2);
Expand All @@ -80,11 +87,18 @@ protected void onCreate(Bundle savedInstanceState) {
mEditorFragment = (EditorFragment) getSupportFragmentManager().findFragmentByTag(savedInstanceState.getString(TAG_EDITOR));
mSettingsFragment = (SettingFragment) getSupportFragmentManager().findFragmentByTag(savedInstanceState.getString(TAG_SETTING));
}

setTitle(getString(R.string.edit));
long noteLocalId = getIntent().getLongExtra(EXT_NOTE_LOCAL_ID, -1);
if (noteLocalId == -1) {
finish();
return;
String action = getIntent().getAction();
if (AccountService.isSignedIn() && TextUtils.equals(action, Intent.ACTION_SEND)) {
Note newNote = getNoteFromShareIntent();
noteLocalId = newNote.getId();
mIsNewNote = true;
} else {
finish();
return;
}
}
EventBus.getDefault().register(this);
mIsNewNote = getIntent().getBooleanExtra(EXT_IS_NEW_NOTE, false);
Expand All @@ -93,6 +107,25 @@ protected void onCreate(Bundle savedInstanceState) {
setResult(RESULT_CANCELED);
}

public Note getNoteFromShareIntent() {
Note newNote = new Note();
Account account = Account.getCurrent();
newNote.setUserId(account.getUserId());
newNote.setTitle(ShareUtils.getSubject(getIntent()));
newNote.setContent(ShareUtils.getBody(getIntent()));
Notebook notebook;
notebook = NotebookDataStore.getRecentNoteBook(account.getUserId());
if (notebook != null) {
newNote.setNoteBookId(notebook.getNotebookId());
} else {
Exception exception = new IllegalStateException("notebook is null");
CrashReport.postCatchedException(exception);
}
newNote.setIsMarkDown(account.getDefaultEditor() == Account.EDITOR_MARKDOWN);
newNote.save();
return newNote;
}

public static Intent getOpenIntent(Context context, long noteLocalId, boolean isNewNote) {
Intent intent = new Intent(context, NoteEditActivity.class);
intent.putExtra(EXT_NOTE_LOCAL_ID, noteLocalId);
Expand Down
52 changes: 52 additions & 0 deletions app/src/main/java/org/houxg/leamonax/utils/ShareUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.houxg.leamonax.utils;

import android.content.Intent;
import android.text.TextUtils;

import static android.content.Intent.ACTION_SEND;
import static android.content.Intent.EXTRA_SUBJECT;
import static android.content.Intent.EXTRA_TEXT;


/**
* Utilities for creating a share intent
*/
public class ShareUtils {

/**
* Create intent with subject and body
*
* @param subject
* @param body
* @return intent
*/
public static Intent create(final CharSequence subject,
final CharSequence body) {
Intent intent = new Intent(ACTION_SEND);
intent.setType("text/plain");
if (!TextUtils.isEmpty(subject))
intent.putExtra(EXTRA_SUBJECT, subject);
intent.putExtra(EXTRA_TEXT, body);
return intent;
}

/**
* Get body from intent
*
* @param intent
* @return body
*/
public static String getBody(final Intent intent) {
return intent != null ? intent.getStringExtra(EXTRA_TEXT) : null;
}

/**
* Get subject from intent
*
* @param intent
* @return subject
*/
public static String getSubject(final Intent intent) {
return intent != null ? intent.getStringExtra(EXTRA_SUBJECT) : null;
}
}

0 comments on commit d25558c

Please sign in to comment.