Example usage for android.content Intent getType

List of usage examples for android.content Intent getType

Introduction

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

Prototype

public @Nullable String getType() 

Source Link

Document

Retrieve any explicit MIME type included in the intent.

Usage

From source file:om.sstvencoder.MainActivity.java

private boolean handleIntent(Intent intent) {
    if (!isIntentTypeValid(intent.getType()) || !isIntentActionValid(intent.getAction()))
        return false;
    Uri uri = intent.hasExtra(Intent.EXTRA_STREAM) ? (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM)
            : intent.getData();// w w  w .j a  v  a 2  s .  co m
    if (uri != null) {
        try {
            ContentResolver resolver = getContentResolver();
            mCropView.setBitmapStream(resolver.openInputStream(uri));
            mCropView.rotateImage(getOrientation(resolver, uri));
            return true;
        } catch (FileNotFoundException ex) {
            if (ex.getCause() instanceof ErrnoException) {
                if (((ErrnoException) ex.getCause()).errno == OsConstants.EACCES) {
                    requestPermissions();
                    return false;
                }
            }
            String s = getString(R.string.load_img_err_title) + ": \n" + ex.getMessage();
            Toast.makeText(this, s, Toast.LENGTH_LONG).show();
        } catch (Exception ex) {
            String s = Utility.createMessage(ex) + "\n\n" + uri + "\n\n" + intent;
            showErrorMessage(getString(R.string.load_img_err_title), ex.getMessage(), s);
        }
    } else {
        String s = getString(R.string.load_img_err_txt_unsupported);
        showErrorMessage(getString(R.string.load_img_err_title), s, s + "\n\n" + intent);
    }
    return false;
}

From source file:com.galois.qrstream.MainActivity.java

private Job buildJobFromIntent(Intent intent) throws IllegalArgumentException {
    String type = intent.getType();
    Bundle extras = intent.getExtras();//from  w w  w  . j a  va2 s .  c  o m
    Log.d(Constants.APP_TAG, "** received type " + type);

    String name = "";
    byte[] bytes = null;

    Uri dataUrl = (Uri) intent.getExtras().getParcelable(Intent.EXTRA_STREAM);
    if (dataUrl != null) {
        name = getNameFromURI(dataUrl);
        if (dataUrl.getScheme().equals("content") || dataUrl.getScheme().equals("file")) {
            try {
                bytes = readFileUri(dataUrl);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            Log.d(Constants.APP_TAG, "unsupported url: " + dataUrl);
        }
    } else {
        // fall back to content in extras (mime type dependent)
        if (type.equals("text/plain")) {
            String subject = extras.getString(Intent.EXTRA_SUBJECT);
            String text = extras.getString(Intent.EXTRA_TEXT);
            if (subject == null) {
                bytes = text.getBytes();
            } else {
                bytes = encodeSubjectAndText(subject, text);
                type = Constants.MIME_TYPE_TEXT_NOTE;
            }
        }
    }
    return new Job(name, bytes, type);
}

From source file:com.Jsu.framework.image.imageChooser.BChooser.java

protected boolean wasVideoSelected(Intent data) {
    if (data == null) {
        return false;
    }/*from  ww w  .ja v  a2  s . co m*/

    if (data.getType() != null && data.getType().startsWith("video")) {
        return true;
    }

    ContentResolver cR = getContext().getContentResolver();
    String type = cR.getType(data.getData());
    return type != null && type.startsWith("video");

}

From source file:com.duy.pascal.ui.activities.SplashScreenActivity.java

/**
 * If receive data from other app (it could be file, text from clipboard),
 * You will be handle data and send to {@link EditorActivity}
 *//*from  w w  w  .ja  v a 2  s.  c o  m*/
private void startMainActivity() {
    Intent data = getIntent();
    String action = data.getAction();
    DLog.d(TAG, "startMainActivity: action = " + action);

    String type = data.getType();
    Intent intentEdit = new Intent(this, EditorActivity.class);
    if (action != null && Intent.ACTION_SEND.equals(action) && type != null) {
        FirebaseAnalytics.getInstance(this).logEvent("open_from_clipboard", new Bundle());
        if (type.equals("text/plain")) {
            handleActionSend(data, intentEdit);
        }
    } else if (action != null && Intent.ACTION_VIEW.equals(action) && type != null) {
        FirebaseAnalytics.getInstance(this).logEvent("open_from_another", new Bundle());
        handleActionView(data, intentEdit);
    } else if (action != null && action.equalsIgnoreCase("run_from_shortcut")) {
        FirebaseAnalytics.getInstance(this).logEvent("run_from_shortcut", new Bundle());
        handleRunProgram(data);
        return;
    }

    intentEdit.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    startActivity(intentEdit);
    overridePendingTransition(0, 0);
    finish();

}

From source file:org.sufficientlysecure.keychain.ui.EncryptTextActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setFullScreenDialogClose(Activity.RESULT_OK, false);

    Intent intent = getIntent();
    String action = intent.getAction();
    Bundle extras = intent.getExtras();//ww  w.  j  a va 2 s.  c o  m
    String type = intent.getType();

    if (extras == null) {
        extras = new Bundle();
    }

    String textData = extras.getString(EXTRA_TEXT);
    boolean returnProcessText = false;

    // When sending to OpenKeychain Encrypt via share menu
    if (Intent.ACTION_SEND.equals(action) && type != null) {
        Log.logDebugBundle(extras, "extras");

        // When sending to OpenKeychain Encrypt via share menu
        if (!MimeUtil.isSameMimeType("text/plain", type)) {
            Toast.makeText(this, R.string.toast_wrong_mimetype, Toast.LENGTH_LONG).show();
            finish();
            return;
        }

        String sharedText;
        if (extras.containsKey(Intent.EXTRA_TEXT)) {
            sharedText = extras.getString(Intent.EXTRA_TEXT);
        } else if (extras.containsKey(Intent.EXTRA_STREAM)) {
            try {
                sharedText = FileHelper.readTextFromUri(this, extras.<Uri>getParcelable(Intent.EXTRA_STREAM),
                        null);
            } catch (IOException e) {
                Toast.makeText(this, R.string.error_preparing_data, Toast.LENGTH_LONG).show();
                finish();
                return;
            }
        } else {
            Toast.makeText(this, R.string.toast_no_text, Toast.LENGTH_LONG).show();
            finish();
            return;
        }

        if (sharedText != null) {
            if (sharedText.length() > Constants.TEXT_LENGTH_LIMIT) {
                sharedText = sharedText.substring(0, Constants.TEXT_LENGTH_LIMIT);
                Notify.create(this, R.string.snack_shared_text_too_long, Style.WARN).show();
            }
            // handle like normal text encryption, override action and extras to later
            // executeServiceMethod ACTION_ENCRYPT_TEXT in main actions
            textData = sharedText;
        }

    }

    // Android 6, PROCESS_TEXT Intent
    if (Intent.ACTION_PROCESS_TEXT.equals(action) && type != null) {

        String sharedText = null;
        if (extras.containsKey(Intent.EXTRA_PROCESS_TEXT)) {
            sharedText = extras.getString(Intent.EXTRA_PROCESS_TEXT);
            returnProcessText = true;
        } else if (extras.containsKey(Intent.EXTRA_PROCESS_TEXT_READONLY)) {
            sharedText = extras.getString(Intent.EXTRA_PROCESS_TEXT_READONLY);
        }

        if (sharedText != null) {
            if (sharedText.length() > Constants.TEXT_LENGTH_LIMIT) {
                sharedText = sharedText.substring(0, Constants.TEXT_LENGTH_LIMIT);
                Notify.create(this, R.string.snack_shared_text_too_long, Style.WARN).show();
            }
            // handle like normal text encryption, override action and extras to later
            // executeServiceMethod ACTION_ENCRYPT_TEXT in main actions
            textData = sharedText;
        }
    }

    if (textData == null) {
        textData = "";
    }

    if (savedInstanceState == null) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        EncryptTextFragment encryptFragment = EncryptTextFragment.newInstance(textData, returnProcessText);
        transaction.replace(R.id.encrypt_text_container, encryptFragment);
        transaction.commit();
    }

}

From source file:com.android.gallery3d.app.Gallery.java

private String getContentType(Intent intent) {
    String type = intent.getType();
    if (type != null)
        return type;

    Uri uri = intent.getData();/*from  w  w  w . j a  va2s. c  om*/
    try {
        return getContentResolver().getType(uri);
    } catch (Throwable t) {
        Log.w(TAG, "get type fail", t);
        return null;
    }
}

From source file:org.sufficientlysecure.keychain.ui.DecryptActivity.java

/**
 * Handles all actions with this intent/*from  ww  w  . jav  a  2  s .  c om*/
 *
 * @param intent
 */
private void handleActions(Intent intent) {
    String action = intent.getAction();
    Bundle extras = intent.getExtras();
    String type = intent.getType();
    Uri uri = intent.getData();

    if (extras == null) {
        extras = new Bundle();
    }

    /*
     * Android's Action
     */
    if (Intent.ACTION_SEND.equals(action) && type != null) {
        // When sending to Keychain Decrypt via share menu
        if ("text/plain".equals(type)) {
            // Plain text
            String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
            if (sharedText != null) {
                // handle like normal text decryption, override action and extras to later
                // executeServiceMethod ACTION_DECRYPT in main actions
                extras.putString(EXTRA_TEXT, sharedText);
                action = ACTION_DECRYPT;
            }
        } else {
            // Binary via content provider (could also be files)
            // override uri to get stream from send
            uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
            action = ACTION_DECRYPT;
        }
    } else if (Intent.ACTION_VIEW.equals(action)) {
        // Android's Action when opening file associated to Keychain (see AndroidManifest.xml)

        // override action
        action = ACTION_DECRYPT;
    }

    String textData = extras.getString(EXTRA_TEXT);

    /**
     * Main Actions
     */
    if (ACTION_DECRYPT.equals(action) && textData != null) {
        Log.d(Constants.TAG, "textData not null, matching text ...");
        Matcher matcher = PgpHelper.PGP_MESSAGE.matcher(textData);
        if (matcher.matches()) {
            Log.d(Constants.TAG, "PGP_MESSAGE matched");
            textData = matcher.group(1);
            // replace non breakable spaces
            textData = textData.replaceAll("\\xa0", " ");

            mMessageFragmentBundle.putString(DecryptMessageFragment.ARG_CIPHERTEXT, textData);
            mSwitchToTab = PAGER_TAB_MESSAGE;
        } else {
            matcher = PgpHelper.PGP_CLEARTEXT_SIGNATURE.matcher(textData);
            if (matcher.matches()) {
                Log.d(Constants.TAG, "PGP_CLEARTEXT_SIGNATURE matched");
                textData = matcher.group(1);
                // replace non breakable spaces
                textData = textData.replaceAll("\\xa0", " ");

                mMessageFragmentBundle.putString(DecryptMessageFragment.ARG_CIPHERTEXT, textData);
                mSwitchToTab = PAGER_TAB_MESSAGE;
            } else {
                Log.d(Constants.TAG, "Nothing matched!");
            }
        }
    } else if (ACTION_DECRYPT.equals(action) && uri != null) {
        // get file path from uri
        String path = FileHelper.getPath(this, uri);

        if (path != null) {
            mFileFragmentBundle.putString(DecryptFileFragment.ARG_FILENAME, path);
            mSwitchToTab = PAGER_TAB_FILE;
        } else {
            Log.e(Constants.TAG, "Direct binary data without actual file in filesystem is not supported. "
                    + "Please use the Remote Service API!");
            Toast.makeText(this, R.string.error_only_files_are_supported, Toast.LENGTH_LONG).show();
            // end activity
            finish();
        }
    } else {
        Log.e(Constants.TAG, "Include the extra 'text' or an Uri with setData() in your Intent!");
    }
}

From source file:com.notepadlite.NoteEditActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_note_edit);

    // Apply theme
    SharedPreferences pref = getSharedPreferences(getPackageName() + "_preferences", Context.MODE_PRIVATE);
    String theme = pref.getString("theme", "light-sans");

    LinearLayout noteViewEdit = (LinearLayout) findViewById(R.id.noteViewEdit);

    if (theme.contains("light"))
        noteViewEdit.setBackgroundColor(getResources().getColor(R.color.window_background));

    if (theme.contains("dark"))
        noteViewEdit.setBackgroundColor(getResources().getColor(R.color.window_background_dark));

    // Set action bar elevation
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        getSupportActionBar().setElevation(getResources().getDimensionPixelSize(R.dimen.action_bar_elevation));

    if (!(getSupportFragmentManager().findFragmentById(R.id.noteViewEdit) instanceof NoteEditFragment)) {
        // Handle intents
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        // Intent sent through an external application
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                external = intent.getStringExtra(Intent.EXTRA_TEXT);

                Bundle bundle = new Bundle();
                bundle.putString("filename", "new");

                Fragment fragment = new NoteEditFragment();
                fragment.setArguments(bundle);

                // Add NoteEditFragment
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.noteViewEdit, fragment, "NoteEditFragment").commit();
            } else {
                showToast(R.string.loading_external_file);
                finish();//  w  ww . j  a  v a2s .c om
            }

            // Intent sent through Google Now "note to self"
        } else if ("com.google.android.gm.action.AUTO_SEND".equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                external = intent.getStringExtra(Intent.EXTRA_TEXT);
                if (external != null) {
                    try {
                        // Write note to disk
                        FileOutputStream output = openFileOutput(String.valueOf(System.currentTimeMillis()),
                                Context.MODE_PRIVATE);
                        output.write(external.getBytes());
                        output.close();

                        // Show toast notification and finish
                        showToast(R.string.note_saved);
                        finish();
                    } catch (IOException e) {
                        // Show error message as toast if file fails to save
                        showToast(R.string.failed_to_save);
                        finish();
                    }
                }
            }
        } else
            finish();
    }
}

From source file:com.bonsai.wallet32.PairWalletActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    mRes = getApplicationContext().getResources();
    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pair_wallet);

    // Set the state of the reduce false positives checkbox.
    boolean reduceFalsePositives = mPrefs.getBoolean("pref_reduceBloomFalsePositives", false);
    CheckBox chkbx = (CheckBox) findViewById(R.id.reduce_false_positives);
    chkbx.setChecked(reduceFalsePositives);
    chkbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override/*from  www .j  av a 2s . c  o  m*/
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putBoolean("pref_reduceBloomFalsePositives", isChecked);
            editor.commit();
        }
    });

    // Hide the reduce bloom false positives if experimental off.
    Boolean isExperimental = mPrefs.getBoolean(SettingsActivity.KEY_EXPERIMENTAL, false);
    if (!isExperimental) {
        findViewById(R.id.reduce_false_positives).setVisibility(View.GONE);
        findViewById(R.id.reduce_space).setVisibility(View.GONE);
    }

    if (savedInstanceState == null) {
        final Intent intent = this.getIntent();
        final String action = intent.getAction();
        final String mimeType = intent.getType();

        if ((NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
                && Nfc.MIMETYPE_WALLET32PAIRING.equals(mimeType)) {
            final NdefMessage ndefMessage = (NdefMessage) intent
                    .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
            final byte[] ndefMessagePayload = Nfc.extractMimePayload(Nfc.MIMETYPE_WALLET32PAIRING, ndefMessage);
            JSONObject codeObj;

            try {
                String msg = new String(ndefMessagePayload, "UTF-8");
                codeObj = new JSONObject(msg);
            } catch (Exception ex) {
                String msg = "trouble deserializing pairing code: " + ex.toString() + " : "
                        + ndefMessagePayload.toString();
                mLogger.error(msg);
                Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
                return;
            }

            // Setup the wallet in a background task.
            new PairWalletTask().execute(codeObj);
        }
    }
}

From source file:com.dimasdanz.kendalipintu.NFCOpenDoorActivity.java

private void handleIntent(Intent intent) {
    String action = intent.getAction();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        String type = intent.getType();
        if (MIME_TEXT_PLAIN.equals(type)) {
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            if (SharedPreferencesManager.getLoggedInPrefs(getApplicationContext())) {
                new NdefReaderTask().execute(tag);
            } else {
                Intent i = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(i);//from  w ww .  j  ava 2s.  c om
            }
        } else {
            Log.d(TAG, "Wrong mime type: " + type);
        }
    }
}