mirror of
https://github.com/leanote/leanote-android.git
synced 2026-01-14 07:00:23 +08:00
Support share content to Leanote
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -13,6 +13,7 @@ import android.view.MenuItem;
|
||||
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;
|
||||
@@ -21,15 +22,20 @@ import org.houxg.leamonax.Leamonax;
|
||||
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;
|
||||
|
||||
@@ -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);
|
||||
@@ -80,11 +87,18 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
|
||||
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);
|
||||
@@ -93,6 +107,25 @@ public class NoteEditActivity extends BaseActivity implements EditorFragment.Edi
|
||||
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);
|
||||
|
||||
52
app/src/main/java/org/houxg/leamonax/utils/ShareUtils.java
Normal file
52
app/src/main/java/org/houxg/leamonax/utils/ShareUtils.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user