Example usage for android.content Intent getScheme

List of usage examples for android.content Intent getScheme

Introduction

In this page you can find the example usage for android.content Intent getScheme.

Prototype

public @Nullable String getScheme() 

Source Link

Document

Return the scheme portion of the intent's data.

Usage

From source file:jackpal.androidterm.RemoteInterface.java

protected void handleIntent() {
    TermService service = getTermService();
    if (service == null) {
        finish();/*  w  w  w.  j  a va2s  . c  o  m*/
        return;
    }

    Intent myIntent = getIntent();
    String action = myIntent.getAction();

    if (action == null) {
        finish();
        return;
    }
    ClipData clipData = myIntent.getClipData();
    if ((AndroidCompat.SDK >= 19 && action.equals(Intent.ACTION_SEND) && myIntent.hasExtra(Intent.EXTRA_STREAM))
            || (action.equals(Intent.ACTION_SEND) && clipData != null)
            || (action.equals("android.intent.action.VIEW")) || (action.equals("android.intent.action.EDIT"))
            || (action.equals("android.intent.action.PICK"))
            || (action.equals("com.googlecode.android_scripting.action.EDIT_SCRIPT"))) {
        String url = null;
        Uri uri = null;
        if (clipData != null) {
            uri = clipData.getItemAt(0).getUri();
            if (uri == null) {
                openText(clipData.getItemAt(0).getText());
                finish();
                return;
            }
        } else if (AndroidCompat.SDK >= 19 && action.equals(Intent.ACTION_SEND)
                && myIntent.hasExtra(Intent.EXTRA_STREAM)) {
            Object extraStream = myIntent.getExtras().get(Intent.EXTRA_STREAM);
            if (extraStream instanceof Uri) {
                uri = (Uri) extraStream;
            }
        } else {
            uri = myIntent.getData();
        }
        String intentCommand = "sh ";
        if (mSettings.getInitialCommand().matches("(.|\n)*(^|\n)-vim\\.app(.|\n)*")) {
            intentCommand = ":e ";
        }
        boolean flavorVim = intentCommand.matches("^:.*");
        if (uri != null && uri.toString().matches("^file:///.*") && flavorVim) {
            String path = uri.getPath();
            if (new File(path).canRead()) {
                path = path.replaceAll(Term.SHELL_ESCAPE, "\\\\$1");
                String command = "\u001b" + intentCommand + path;
                // Find the target window
                mReplace = true;
                mHandle = switchToWindow(mHandle, command);
                mReplace = false;
            }
            finish();
        } else if (AndroidCompat.SDK >= 19 && uri != null && uri.getScheme() != null
                && uri.getScheme().equals("content") && flavorVim) {
            Context context = this;
            String command = null;
            String path = Term.getPath(context, uri);
            if (path != null) {
                path = path.replaceAll(Term.SHELL_ESCAPE, "\\\\$1");
                command = "\u001b" + intentCommand + path;
            } else if (getContentResolver() != null) {
                try {
                    Cursor cursor = getContentResolver().query(uri, null, null, null, null, null);
                    path = Term.handleOpenDocument(uri, cursor);
                } catch (Exception e) {
                    alert(e.toString() + "\n" + this.getString(R.string.storage_read_error));
                    finish();
                }
                if (path == null) {
                    alert(this.getString(R.string.storage_read_error));
                    finish();
                } else {
                    File dir = Term.getScratchCacheDir(this);
                    SyncFileObserver sfo = new SyncFileObserver(path);
                    sfo.setConTentResolver(this.getContentResolver());
                    path = dir.toString() + path;
                    String fname = new File(path).getName();
                    if (path.equals("") || !sfo.putUriAndLoad(uri, path)) {
                        alert(fname + "\n" + this.getString(R.string.storage_read_error));
                        finish();
                    }
                    path = path.replaceAll(Term.SHELL_ESCAPE, "\\\\$1");
                    command = "\u001b" + intentCommand + path;
                }
            }
            // Find the target window
            mReplace = true;
            mHandle = switchToWindow(mHandle, command);
            mReplace = false;
            finish();
        } else if (action.equals("com.googlecode.android_scripting.action.EDIT_SCRIPT")) {
            url = myIntent.getExtras().getString("com.googlecode.android_scripting.extra.SCRIPT_PATH");
        } else if (myIntent.getScheme() != null && myIntent.getScheme() != null
                && myIntent.getScheme().equals("file")) {
            if (myIntent.getData() != null)
                url = myIntent.getData().getPath();
        }
        if (url != null) {
            String command = "sh ";
            if (mSettings.getInitialCommand().matches("(.|\n)*(^|\n)-vim\\.app(.|\n)*")) {
                command = ":e ";
            }
            if (command.matches("^:.*")) {
                url = url.replaceAll(Term.SHELL_ESCAPE, "\\\\$1");
                command = "\u001b" + command + url;
                // Find the target window
                mReplace = true;
                mHandle = switchToWindow(mHandle, command);
                mReplace = false;
            } else if ((mHandle != null) && (url.equals(mFname))) {
                // Target the request at an existing window if open
                command = command + url;
                mHandle = switchToWindow(mHandle, command);
            } else {
                // Open a new window
                command = command + url;
                mHandle = openNewWindow(command);
            }
            mFname = url;

            Intent result = new Intent();
            result.putExtra(EXTRA_WINDOW_HANDLE, mHandle);
            setResult(RESULT_OK, result);
        }
    } else if (action.equals(Intent.ACTION_SEND) && myIntent.hasExtra(Intent.EXTRA_TEXT)) {
        openText(myIntent.getExtras().getCharSequence(Intent.EXTRA_TEXT));
    } else {
    }

    finish();
}

From source file:com.if3games.chessonline.DroidFish.java

/**
 * Return PGN/FEN data or filename from the Intent. Both can not be non-null.
 * @return Pair of PGN/FEN data and filename.
 *//*from   ww w  .  j  av a2 s  . c  om*/
private final Pair<String, String> getPgnOrFenIntent() {
    String pgnOrFen = null;
    String filename = null;
    try {
        Intent intent = getIntent();
        Uri data = intent.getData();
        if (data == null) {
            Bundle b = intent.getExtras();
            if (b != null) {
                Object strm = b.get(Intent.EXTRA_STREAM);
                if (strm instanceof Uri) {
                    data = (Uri) strm;
                    if ("file".equals(data.getScheme())) {
                        filename = data.getEncodedPath();
                        if (filename != null)
                            filename = Uri.decode(filename);
                    }
                }
            }
        }
        if (data == null) {
            if ((Intent.ACTION_SEND.equals(intent.getAction()) || Intent.ACTION_VIEW.equals(intent.getAction()))
                    && ("application/x-chess-pgn".equals(intent.getType())
                            || "application/x-chess-fen".equals(intent.getType())))
                pgnOrFen = intent.getStringExtra(Intent.EXTRA_TEXT);
        } else {
            String scheme = intent.getScheme();
            if ("file".equals(scheme)) {
                filename = data.getEncodedPath();
                if (filename != null)
                    filename = Uri.decode(filename);
            }
            if ((filename == null) && ("content".equals(scheme) || "file".equals(scheme))) {
                ContentResolver resolver = getContentResolver();
                InputStream in = resolver.openInputStream(intent.getData());
                StringBuilder sb = new StringBuilder();
                while (true) {
                    byte[] buffer = new byte[16384];
                    int len = in.read(buffer);
                    if (len <= 0)
                        break;
                    sb.append(new String(buffer, 0, len));
                }
                pgnOrFen = sb.toString();
            }
        }
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), R.string.failed_to_read_pgn_data, Toast.LENGTH_SHORT).show();
    }
    return new Pair<String, String>(pgnOrFen, filename);
}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public String getAppArg() {
    if (super.getAppArg() != null) {
        // This just maintains backward compatibility in case people are manually
        // setting the AppArg in their properties.  It reproduces the general
        // behaviour the existed when AppArg was just another Display property.
        return super.getAppArg();
    }/*from  ww  w  .j  a  v  a 2  s  . co m*/
    if (getActivity() == null) {
        return null;
    }

    android.content.Intent intent = getActivity().getIntent();
    if (intent != null) {
        Uri u = intent.getData();
        String scheme = intent.getScheme();
        if (u == null && intent.getExtras() != null) {
            if (intent.getExtras().keySet().contains("android.intent.extra.STREAM")) {
                try {
                    u = (Uri) intent.getParcelableExtra("android.intent.extra.STREAM");
                    scheme = u.getScheme();
                    System.out.println("u=" + u);
                } catch (Exception ex) {
                    Log.d("Codename One", "Failed to load parcelable extra from intent: " + ex.getMessage());
                }
            }

        }
        if (u != null) {
            //String scheme = intent.getScheme();
            intent.setData(null);
            if ("content".equals(scheme)) {
                try {
                    InputStream attachment = getActivity().getContentResolver().openInputStream(u);
                    if (attachment != null) {
                        String name = getContentName(getActivity().getContentResolver(), u);
                        if (name != null) {
                            String filePath = getAppHomePath() + getFileSystemSeparator() + name;
                            File f = new File(filePath);
                            OutputStream tmp = createFileOuputStream(f);
                            byte[] buffer = new byte[1024];
                            int read = -1;
                            while ((read = attachment.read(buffer)) > -1) {
                                tmp.write(buffer, 0, read);
                            }
                            tmp.close();
                            attachment.close();
                            setAppArg(filePath);
                            return filePath;
                        }
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            } else {

                /*
                // Why do we need this special case?  u.toString()
                // will include the full URL including query string.
                // This special case causes urls like myscheme://part1/part2
                // to only return "/part2" which is obviously problematic and
                // is inconsistent with iOS.  Is this special case necessary
                // in some versions of Android?
                String encodedPath = u.getEncodedPath();
                if (encodedPath != null && encodedPath.length() > 0) {
                String query = u.getQuery();
                if(query != null && query.length() > 0){
                    encodedPath += "?" + query;
                }
                setAppArg(encodedPath);
                return encodedPath;
                }
                */
                setAppArg(u.toString());
                return u.toString();
            }
        }
    }
    return null;
}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (requestCode == ZOOZ_PAYMENT) {
        ((IntentResultListener) pur).onActivityResult(requestCode, resultCode, intent);
        return;//w w  w .  j  a  v a  2s  .  c  o  m
    }

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CAPTURE_IMAGE) {
            try {
                String imageUri = (String) Storage.getInstance().readObject("imageUri");
                Vector pathandId = StringUtil.tokenizeString(imageUri, ";");
                String path = (String) pathandId.get(0);
                String lastId = (String) pathandId.get(1);
                Storage.getInstance().deleteStorageFile("imageUri");
                clearMediaDB(lastId, path);
                callback.fireActionEvent(new ActionEvent(path));
                return;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == CAPTURE_VIDEO) {
            String path = (String) Storage.getInstance().readObject("videoUri");
            Storage.getInstance().deleteStorageFile("videoUri");
            callback.fireActionEvent(new ActionEvent(path));
            return;
        } else if (requestCode == CAPTURE_AUDIO) {
            Uri data = intent.getData();
            String path = convertImageUriToFilePath(data, getContext());
            callback.fireActionEvent(new ActionEvent(path));
            return;

        } else if (requestCode == OPEN_GALLERY_MULTI) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                if (intent.getClipData() != null) {
                    // If it was a multi-request
                    ArrayList<String> selectedPaths = new ArrayList<String>();
                    int count = intent.getClipData().getItemCount();
                    for (int i = 0; i < count; i++) {

                        Uri uri = intent.getClipData().getItemAt(i).getUri();
                        String p = getImageFilePath(uri);
                        if (p != null) {
                            selectedPaths.add(p);
                        }
                    }
                    callback.fireActionEvent(
                            new ActionEvent(selectedPaths.toArray(new String[selectedPaths.size()])));
                    return;
                }
            } else {
                com.codename1.io.Log.e(new RuntimeException(
                        "OPEN_GALLERY_MULTI requires android sdk 16 (jelly bean) or higher"));
                callback.fireActionEvent(null);
            }

            Uri selectedImage = intent.getData();
            String scheme = intent.getScheme();

            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null,
                    null);

            // this happens on Android devices, not exactly sure what the use case is
            if (cursor == null) {
                callback.fireActionEvent(null);
                return;
            }

            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

            if (filePath == null && "content".equals(scheme)) {
                //if the file is not on the filesystem download it and save it
                //locally
                try {
                    InputStream inputStream = getContext().getContentResolver().openInputStream(selectedImage);
                    if (inputStream != null) {
                        String name = getContentName(getContext().getContentResolver(), selectedImage);
                        if (name != null) {
                            filePath = getAppHomePath() + getFileSystemSeparator() + name;
                            File f = new File(removeFilePrefix(filePath));
                            OutputStream tmp = createFileOuputStream(f);
                            byte[] buffer = new byte[1024];
                            int read = -1;
                            while ((read = inputStream.read(buffer)) > -1) {
                                tmp.write(buffer, 0, read);
                            }
                            tmp.close();
                            inputStream.close();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            callback.fireActionEvent(new ActionEvent(new String[] { filePath }));
            return;
        } else if (requestCode == OPEN_GALLERY) {

            Uri selectedImage = intent.getData();
            String scheme = intent.getScheme();

            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null,
                    null);

            // this happens on Android devices, not exactly sure what the use case is
            if (cursor == null) {
                callback.fireActionEvent(null);
                return;
            }

            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

            if (filePath == null && "content".equals(scheme)) {
                //if the file is not on the filesystem download it and save it
                //locally
                try {
                    InputStream inputStream = getContext().getContentResolver().openInputStream(selectedImage);
                    if (inputStream != null) {
                        String name = getContentName(getContext().getContentResolver(), selectedImage);
                        if (name != null) {
                            filePath = getAppHomePath() + getFileSystemSeparator() + name;
                            File f = new File(removeFilePrefix(filePath));
                            OutputStream tmp = createFileOuputStream(f);
                            byte[] buffer = new byte[1024];
                            int read = -1;
                            while ((read = inputStream.read(buffer)) > -1) {
                                tmp.write(buffer, 0, read);
                            }
                            tmp.close();
                            inputStream.close();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            callback.fireActionEvent(new ActionEvent(filePath));
            return;
        } else {
            if (callback != null) {
                callback.fireActionEvent(new ActionEvent("ok"));
            }
            return;
        }
    }
    //clean imageUri
    String imageUri = (String) Storage.getInstance().readObject("imageUri");
    if (imageUri != null) {
        Storage.getInstance().deleteStorageFile("imageUri");
    }

    if (callback != null) {
        callback.fireActionEvent(null);
    }
}