Java Code Examples for com.example.android.common.logger.Log#w()

The following examples show how to use com.example.android.common.logger.Log#w() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PermissionRequestFragment.java    From android-PermissionRequest with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onConsoleMessage(@NonNull ConsoleMessage message) {
    switch (message.messageLevel()) {
        case TIP:
            Log.v(TAG, message.message());
            break;
        case LOG:
            Log.i(TAG, message.message());
            break;
        case WARNING:
            Log.w(TAG, message.message());
            break;
        case ERROR:
            Log.e(TAG, message.message());
            break;
        case DEBUG:
            Log.d(TAG, message.message());
            break;
    }
    if (null != mConsoleMonitor) {
        mConsoleMonitor.onConsoleMessage(message);
    }
    return true;
}
 
Example 2
Source File: BatchStepSensorFragment.java    From sensors-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Register a {@link android.hardware.SensorEventListener} for the sensor and max batch delay.
 * The maximum batch delay specifies the maximum duration in microseconds for which subsequent
 * sensor events can be temporarily stored by the sensor before they are delivered to the
 * registered SensorEventListener. A larger delay allows the system to handle sensor events more
 * efficiently, allowing the system to switch to a lower power state while the sensor is
 * capturing events. Once the max delay is reached, all stored events are delivered to the
 * registered listener. Note that this value only specifies the maximum delay, the listener may
 * receive events quicker. A delay of 0 disables batch mode and registers the listener in
 * continuous mode.
 * The optimium batch delay depends on the application. For example, a delay of 5 seconds or
 * higher may be appropriate for an  application that does not update the UI in real time.
 *
 * @param maxdelay
 * @param sensorType
 */
private void registerEventListener(int maxdelay, int sensorType) {
    // BEGIN_INCLUDE(register)

    // Keep track of state so that the correct sensor type and batch delay can be set up when
    // the app is restored (for example on screen rotation).
    mMaxDelay = maxdelay;
    if (sensorType == Sensor.TYPE_STEP_COUNTER) {
        mState = STATE_COUNTER;
        /*
        Reset the initial step counter value, the first event received by the event listener is
        stored in mCounterSteps and used to calculate the total number of steps taken.
         */
        mCounterSteps = 0;
        Log.i(TAG, "Event listener for step counter sensor registered with a max delay of "
                + mMaxDelay);
    } else {
        mState = STATE_DETECTOR;
        Log.i(TAG, "Event listener for step detector sensor registered with a max delay of "
                + mMaxDelay);
    }

    // Get the default sensor for the sensor type from the SenorManager
    SensorManager sensorManager =
            (SensorManager) getActivity().getSystemService(Activity.SENSOR_SERVICE);
    // sensorType is either Sensor.TYPE_STEP_COUNTER or Sensor.TYPE_STEP_DETECTOR
    Sensor sensor = sensorManager.getDefaultSensor(sensorType);

    // Register the listener for this sensor in batch mode.
    // If the max delay is 0, events will be delivered in continuous mode without batching.
    final boolean batchMode = sensorManager.registerListener(
            mListener, sensor, SensorManager.SENSOR_DELAY_NORMAL, maxdelay);

    if (!batchMode) {
        // Batch mode could not be enabled, show a warning message and switch to continuous mode
        getCardStream().getCard(CARD_NOBATCHSUPPORT)
                .setDescription(getString(R.string.warning_nobatching));
        getCardStream().showCard(CARD_NOBATCHSUPPORT);
        Log.w(TAG, "Could not register sensor listener in batch mode, " +
                "falling back to continuous mode.");
    }

    if (maxdelay > 0 && batchMode) {
        // Batch mode was enabled successfully, show a description card
        getCardStream().showCard(CARD_BATCHING_DESCRIPTION);
    }

    // Show the explanation card
    getCardStream().showCard(CARD_EXPLANATION);

    // END_INCLUDE(register)

}
 
Example 3
Source File: BatchStepSensorFragment.java    From android-BatchStepSensor with Apache License 2.0 4 votes vote down vote up
/**
 * Register a {@link android.hardware.SensorEventListener} for the sensor and max batch delay.
 * The maximum batch delay specifies the maximum duration in microseconds for which subsequent
 * sensor events can be temporarily stored by the sensor before they are delivered to the
 * registered SensorEventListener. A larger delay allows the system to handle sensor events more
 * efficiently, allowing the system to switch to a lower power state while the sensor is
 * capturing events. Once the max delay is reached, all stored events are delivered to the
 * registered listener. Note that this value only specifies the maximum delay, the listener may
 * receive events quicker. A delay of 0 disables batch mode and registers the listener in
 * continuous mode.
 * The optimium batch delay depends on the application. For example, a delay of 5 seconds or
 * higher may be appropriate for an  application that does not update the UI in real time.
 *
 * @param maxdelay
 * @param sensorType
 */
private void registerEventListener(int maxdelay, int sensorType) {
    // BEGIN_INCLUDE(register)

    // Keep track of state so that the correct sensor type and batch delay can be set up when
    // the app is restored (for example on screen rotation).
    mMaxDelay = maxdelay;
    if (sensorType == Sensor.TYPE_STEP_COUNTER) {
        mState = STATE_COUNTER;
        /*
        Reset the initial step counter value, the first event received by the event listener is
        stored in mCounterSteps and used to calculate the total number of steps taken.
         */
        mCounterSteps = 0;
        Log.i(TAG, "Event listener for step counter sensor registered with a max delay of "
                + mMaxDelay);
    } else {
        mState = STATE_DETECTOR;
        Log.i(TAG, "Event listener for step detector sensor registered with a max delay of "
                + mMaxDelay);
    }

    // Get the default sensor for the sensor type from the SenorManager
    SensorManager sensorManager =
            (SensorManager) getActivity().getSystemService(Activity.SENSOR_SERVICE);
    // sensorType is either Sensor.TYPE_STEP_COUNTER or Sensor.TYPE_STEP_DETECTOR
    Sensor sensor = sensorManager.getDefaultSensor(sensorType);

    // Register the listener for this sensor in batch mode.
    // If the max delay is 0, events will be delivered in continuous mode without batching.
    final boolean batchMode = sensorManager.registerListener(
            mListener, sensor, SensorManager.SENSOR_DELAY_NORMAL, maxdelay);

    if (!batchMode) {
        // Batch mode could not be enabled, show a warning message and switch to continuous mode
        getCardStream().getCard(CARD_NOBATCHSUPPORT)
                .setDescription(getString(R.string.warning_nobatching));
        getCardStream().showCard(CARD_NOBATCHSUPPORT);
        Log.w(TAG, "Could not register sensor listener in batch mode, " +
                "falling back to continuous mode.");
    }

    if (maxdelay > 0 && batchMode) {
        // Batch mode was enabled successfully, show a description card
        getCardStream().showCard(CARD_BATCHING_DESCRIPTION);
    }

    // Show the explanation card
    getCardStream().showCard(CARD_EXPLANATION);

    // END_INCLUDE(register)

}
 
Example 4
Source File: BasicAndroidKeyStoreFragment.java    From android-BasicAndroidKeyStore with Apache License 2.0 4 votes vote down vote up
/**
 * Signs the data using the key pair stored in the Android Key Store.  This signature can be
 * used with the data later to verify it was signed by this application.
 * @return A string encoding of the data signature generated
 */
public String signData(String inputStr) throws KeyStoreException,
        UnrecoverableEntryException, NoSuchAlgorithmException, InvalidKeyException,
        SignatureException, IOException, CertificateException {
    byte[] data = inputStr.getBytes();

    // BEGIN_INCLUDE(sign_load_keystore)
    KeyStore ks = KeyStore.getInstance(SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);

    // Weird artifact of Java API.  If you don't have an InputStream to load, you still need
    // to call "load", or it'll crash.
    ks.load(null);

    // Load the key pair from the Android Key Store
    KeyStore.Entry entry = ks.getEntry(mAlias, null);

    /* If the entry is null, keys were never stored under this alias.
     * Debug steps in this situation would be:
     * -Check the list of aliases by iterating over Keystore.aliases(), be sure the alias
     *   exists.
     * -If that's empty, verify they were both stored and pulled from the same keystore
     *   "AndroidKeyStore"
     */
    if (entry == null) {
        Log.w(TAG, "No key found under alias: " + mAlias);
        Log.w(TAG, "Exiting signData()...");
        return null;
    }

    /* If entry is not a KeyStore.PrivateKeyEntry, it might have gotten stored in a previous
     * iteration of your application that was using some other mechanism, or been overwritten
     * by something else using the same keystore with the same alias.
     * You can determine the type using entry.getClass() and debug from there.
     */
    if (!(entry instanceof KeyStore.PrivateKeyEntry)) {
        Log.w(TAG, "Not an instance of a PrivateKeyEntry");
        Log.w(TAG, "Exiting signData()...");
        return null;
    }
    // END_INCLUDE(sign_data)

    // BEGIN_INCLUDE(sign_create_signature)
    // This class doesn't actually represent the signature,
    // just the engine for creating/verifying signatures, using
    // the specified algorithm.
    Signature s = Signature.getInstance(SecurityConstants.SIGNATURE_SHA256withRSA);

    // Initialize Signature using specified private key
    s.initSign(((KeyStore.PrivateKeyEntry) entry).getPrivateKey());

    // Sign the data, store the result as a Base64 encoded String.
    s.update(data);
    byte[] signature = s.sign();
    String result = Base64.encodeToString(signature, Base64.DEFAULT);
    // END_INCLUDE(sign_data)

    return result;
}
 
Example 5
Source File: BasicAndroidKeyStoreFragment.java    From android-BasicAndroidKeyStore with Apache License 2.0 4 votes vote down vote up
/**
 * Given some data and a signature, uses the key pair stored in the Android Key Store to verify
 * that the data was signed by this application, using that key pair.
 * @param input The data to be verified.
 * @param signatureStr The signature provided for the data.
 * @return A boolean value telling you whether the signature is valid or not.
 */
public boolean verifyData(String input, String signatureStr) throws KeyStoreException,
        CertificateException, NoSuchAlgorithmException, IOException,
        UnrecoverableEntryException, InvalidKeyException, SignatureException {
    byte[] data = input.getBytes();
    byte[] signature;
    // BEGIN_INCLUDE(decode_signature)

    // Make sure the signature string exists.  If not, bail out, nothing to do.

    if (signatureStr == null) {
        Log.w(TAG, "Invalid signature.");
        Log.w(TAG, "Exiting verifyData()...");
        return false;
    }

    try {
        // The signature is going to be examined as a byte array,
        // not as a base64 encoded string.
        signature = Base64.decode(signatureStr, Base64.DEFAULT);
    } catch (IllegalArgumentException e) {
        // signatureStr wasn't null, but might not have been encoded properly.
        // It's not a valid Base64 string.
        return false;
    }
    // END_INCLUDE(decode_signature)

    KeyStore ks = KeyStore.getInstance("AndroidKeyStore");

    // Weird artifact of Java API.  If you don't have an InputStream to load, you still need
    // to call "load", or it'll crash.
    ks.load(null);

    // Load the key pair from the Android Key Store
    KeyStore.Entry entry = ks.getEntry(mAlias, null);

    if (entry == null) {
        Log.w(TAG, "No key found under alias: " + mAlias);
        Log.w(TAG, "Exiting verifyData()...");
        return false;
    }

    if (!(entry instanceof KeyStore.PrivateKeyEntry)) {
        Log.w(TAG, "Not an instance of a PrivateKeyEntry");
        return false;
    }

    // This class doesn't actually represent the signature,
    // just the engine for creating/verifying signatures, using
    // the specified algorithm.
    Signature s = Signature.getInstance(SecurityConstants.SIGNATURE_SHA256withRSA);

    // BEGIN_INCLUDE(verify_data)
    // Verify the data.
    s.initVerify(((KeyStore.PrivateKeyEntry) entry).getCertificate());
    s.update(data);
    return s.verify(signature);
    // END_INCLUDE(verify_data)
}