Search in sources :

Example 1 with DottedQueryParser

use of com.orgzly.android.query.user.DottedQueryParser in project orgzly-android by orgzly.

the class ShareActivity method getDataFromIntent.

public Data getDataFromIntent(Intent intent) {
    Data data = new Data();
    mError = null;
    String action = intent.getAction();
    String type = intent.getType();
    if (action == null) {
    // mError = getString(R.string.share_action_not_set);
    } else if (type == null) {
    // mError = getString(R.string.share_type_not_set);
    } else if (action.equals(Intent.ACTION_SEND)) {
        if (type.startsWith("text/")) {
            if (intent.hasExtra(Intent.EXTRA_TEXT)) {
                data.title = intent.getStringExtra(Intent.EXTRA_TEXT);
            } else if (intent.hasExtra(Intent.EXTRA_STREAM)) {
                Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
                data.title = uri.getLastPathSegment();
                /*
                     * Store file's content as note content.
                     */
                try {
                    File file = new File(uri.getPath());
                    /* Don't read large files. */
                    if (file.length() > MAX_TEXT_FILE_LENGTH_FOR_CONTENT) {
                        mError = "File has " + file.length() + " bytes (refusing to read files larger then " + MAX_TEXT_FILE_LENGTH_FOR_CONTENT + " bytes)";
                    } else {
                        data.content = MiscUtils.readStringFromFile(file);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    mError = "Failed reading the content of " + uri.toString() + ": " + e.toString();
                }
            }
            if (data.title != null && data.content == null && intent.hasExtra(Intent.EXTRA_SUBJECT)) {
                data.content = data.title;
                data.title = intent.getStringExtra(Intent.EXTRA_SUBJECT);
            }
            if (intent.hasExtra(AppIntent.EXTRA_FILTER)) {
                Query query = new DottedQueryParser().parse(intent.getStringExtra(AppIntent.EXTRA_FILTER));
                String bookName = QueryUtils.INSTANCE.extractFirstBookNameFromQuery(query.getCondition());
                if (bookName != null) {
                    Book book = new Shelf(this).getBook(bookName);
                    if (book != null) {
                        data.bookId = book.getId();
                    }
                }
            }
        } else {
            mError = getString(R.string.share_type_not_supported, type);
        }
    } else if (action.equals("com.google.android.gm.action.AUTO_SEND")) {
        if (type.startsWith("text/") && intent.hasExtra(Intent.EXTRA_TEXT)) {
            data.title = intent.getStringExtra(Intent.EXTRA_TEXT);
        }
    } else {
        mError = getString(R.string.share_action_not_supported, action);
    }
    /* Make sure that title is never empty. */
    if (data.title == null) {
        data.title = "";
    }
    return data;
}
Also used : Query(com.orgzly.android.query.Query) Book(com.orgzly.android.Book) DottedQueryParser(com.orgzly.android.query.user.DottedQueryParser) Shelf(com.orgzly.android.Shelf) IOException(java.io.IOException) Uri(android.net.Uri) File(java.io.File)

Aggregations

Uri (android.net.Uri)1 Book (com.orgzly.android.Book)1 Shelf (com.orgzly.android.Shelf)1 Query (com.orgzly.android.query.Query)1 DottedQueryParser (com.orgzly.android.query.user.DottedQueryParser)1 File (java.io.File)1 IOException (java.io.IOException)1