package org.koreader.launcher;

import android.app.SearchManager;
import android.content.Intent;


public class IntentUtils {

    /* get intent by action type, used to do dict lookups on 3rd party apps.
     *
     * @param text to search
     * @param package that receives the query
     * @param action associated to the package
     *
     * @returns a Intent based on package/action ready to do a text lookup
     */

    public static Intent getByAction(String text, String pkg, String action) {
        Intent intent = new Intent();
        if ("send".equals(action)) {
            // Intent.ACTION_SEND
            intent = new Intent(getSendIntent(text, pkg));
        } else if ("search".equals(action)) {
            // Intent.ACTION_SEARCH
            intent = new Intent(getSearchIntent(text, pkg));
        } else if ("text".equals(action)) {
            // Intent.ACTION_PROCESS_TEXT
            intent = new Intent(getTextIntent(text, pkg));
        } else if ("colordict".equals(action)) {
            // colordict.intent.action.SEARCH
            intent = new Intent(getColordictIntent(text, pkg));
        } else if ("aard2".equals(action)) {
            // aard2.lookup
            intent = new Intent(getAard2Intent(text));
        } else if ("quickdic".equals(action)) {
            // com.hughes.action.ACTION_SEARCH_DICT
            intent = new Intent(getQuickdicIntent(text));
        }
        return intent;
    }

    /* Android common intents -------- */

    // Intent.ACTION_SEND with the app picker
    private static Intent getSendIntent(String text) {
        return getSendIntent(text, null);
    }

    // Intent.ACTION_SEND with a specific package
    private static Intent getSendIntent(String text, String pkg) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_TEXT, text);
        intent.setType("text/plain");
        if (pkg != null) intent.setPackage(pkg);
        return intent;
    }

    // Intent.ACTION_SEARCH with the app picker
    private static Intent getSearchIntent(String text) {
        return getSearchIntent(text, null);
    }

    // Intent.ACTION_SEARCH with a specific package
    private static Intent getSearchIntent(String text, String pkg) {
        Intent intent = new Intent(Intent.ACTION_SEARCH);
        intent.putExtra(SearchManager.QUERY, text);
        intent.putExtra(Intent.EXTRA_TEXT, text);
        if (pkg != null) intent.setPackage(pkg);
        return intent;
    }

    // Intent.ACTION_PROCESS_TEXT with the app picker
    private static Intent getTextIntent(String text) {
        return getTextIntent(text, null);
    }

    // Intent.ACTION_PROCESS_TEXT with a specific package
    private static Intent getTextIntent(String text, String pkg) {
        Intent intent = new Intent(Intent.ACTION_PROCESS_TEXT);
        intent.putExtra(Intent.EXTRA_TEXT, text);
        intent.putExtra(Intent.EXTRA_PROCESS_TEXT, text);
        intent.putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, text);
        intent.setType("text/plain");
        if (pkg != null) intent.setPackage(pkg);
        return intent;
    }

    /* Android custom intents for some dict apps ------ */

    private static Intent getAard2Intent(String text) {
        Intent intent = new Intent("aard2.lookup");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(SearchManager.QUERY, text);
        return intent;
    }

    private static Intent getColordictIntent(String text, String pkg) {
        Intent intent = new Intent("colordict.intent.action.SEARCH");
        intent.putExtra("EXTRA_QUERY", text);
        intent.putExtra("EXTRA_FULLSCREEN", true);
        intent.setPackage(pkg);
        return intent;
    }

    private static Intent getQuickdicIntent(String text) {
        Intent intent = new Intent("com.hughes.action.ACTION_SEARCH_DICT");
        intent.putExtra(SearchManager.QUERY, text);
        return intent;
    }
}

@@ -1389,32 +1389,508 @@
 cIntent(text));%0A
+        %7D else if (%22picker-send%22.equals(action)) %7B%0A            // app picker for Intent.ACTION_SEND%0A            intent = new Intent(getSendIntent(text));%0A        %7D else if (%22picker-search%22.equals(action)) %7B%0A            // app picker for Intent.ACTION_SEARCH%0A            intent = new Intent(getSearchIntent(text));%0A        %7D else if (%22picker-text%22.equals(action)) %7B%0A            // app picker for Intent.ACTION_PROCESS_TEXT%0A            intent = new Intent(getTextIntent(text));%0A
         %7D%0A      

implement app picker actions