Example usage for android.util Log w

List of usage examples for android.util Log w

Introduction

In this page you can find the example usage for android.util Log w.

Prototype

public static int w(String tag, Throwable tr) 

Source Link

Usage

From source file:Main.java

public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) {

    List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes();
    if (rawSupportedSizes == null) {
        Log.w(TAG, "Device returned no supported preview sizes; using default");
        Camera.Size defaultSize = parameters.getPreviewSize();
        if (defaultSize == null) {
            throw new IllegalStateException("Parameters contained no preview size!");
        }//from  w ww . ja  v a 2s  .co m
        return new Point(defaultSize.width, defaultSize.height);
    }

    // Sort by size, descending
    List<Camera.Size> supportedPreviewSizes = new ArrayList<>(rawSupportedSizes);
    Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() {
        @Override
        public int compare(Camera.Size a, Camera.Size b) {
            int aPixels = a.height * a.width;
            int bPixels = b.height * b.width;
            Log.d(TAG, "compare: camera's height:" + a.height + "camera's width:" + a.width);
            if (bPixels < aPixels) {
                return -1;
            }
            if (bPixels > aPixels) {
                return 1;
            }
            return 0;
        }
    });

    if (Log.isLoggable(TAG, Log.INFO)) {
        StringBuilder previewSizesString = new StringBuilder();
        for (Camera.Size supportedPreviewSize : supportedPreviewSizes) {
            previewSizesString.append(supportedPreviewSize.width).append('x')
                    .append(supportedPreviewSize.height).append(' ');
        }
        Log.i(TAG, "Supported preview sizes: " + previewSizesString);
    }

    double screenAspectRatio = screenResolution.x / (double) screenResolution.y;

    // Remove sizes that are unsuitable
    Iterator<Camera.Size> it = supportedPreviewSizes.iterator();
    while (it.hasNext()) {
        Camera.Size supportedPreviewSize = it.next();
        int realWidth = supportedPreviewSize.width;
        int realHeight = supportedPreviewSize.height;
        if (realWidth * realHeight < MIN_PREVIEW_PIXELS) {
            it.remove();
            continue;
        }

        boolean isCandidatePortrait = realWidth < realHeight;
        Log.d(TAG, "isCandidatePortrait(realWidth < realHeight): " + isCandidatePortrait);
        int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth;
        int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight;
        double aspectRatio = maybeFlippedWidth / (double) maybeFlippedHeight;
        double distortion = Math.abs(aspectRatio - screenAspectRatio);
        if (distortion > MAX_ASPECT_DISTORTION) {
            it.remove();
            continue;
        }

        if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {
            Point exactPoint = new Point(realWidth, realHeight);
            Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);
            return exactPoint;
        }
    }

    // If no exact match, use largest preview size. This was not a great idea on older devices because
    // of the additional computation needed. We're likely to get here on newer Android 4+ devices, where
    // the CPU is much more powerful.
    if (!supportedPreviewSizes.isEmpty()) {
        Camera.Size largestPreview = supportedPreviewSizes.get(0);
        Point largestSize = new Point(largestPreview.width, largestPreview.height);
        Log.i(TAG, "Using largest suitable preview size: " + largestSize);
        return largestSize;
    }

    // If there is nothing at all suitable, return current preview size
    Camera.Size defaultPreview = parameters.getPreviewSize();
    if (defaultPreview == null) {
        throw new IllegalStateException("Parameters contained no preview size!");
    }
    Point defaultSize = new Point(defaultPreview.width, defaultPreview.height);
    Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize);
    return defaultSize;
}

From source file:Main.java

public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) {

    List<Size> rawSupportedSizes = parameters.getSupportedPreviewSizes();
    if (rawSupportedSizes == null) {
        Log.w(TAG, "Device returned no supported preview sizes; using default");
        Size defaultSize = parameters.getPreviewSize();
        if (defaultSize == null) {
            throw new IllegalStateException("Parameters contained no preview size!");
        }/*from w w  w .  j ava 2  s  .  co  m*/
        return new Point(defaultSize.width, defaultSize.height);
    }

    // Sort by size, descending
    List<Size> supportedPreviewSizes = new ArrayList<Size>(rawSupportedSizes);
    Collections.sort(supportedPreviewSizes, new Comparator<Size>() {
        @Override
        public int compare(Size a, Size b) {
            int aPixels = a.height * a.width;
            int bPixels = b.height * b.width;
            if (bPixels < aPixels) {
                return -1;
            }
            if (bPixels > aPixels) {
                return 1;
            }
            return 0;
        }
    });

    if (Log.isLoggable(TAG, Log.INFO)) {
        StringBuilder previewSizesString = new StringBuilder();
        for (Size supportedPreviewSize : supportedPreviewSizes) {
            previewSizesString.append(supportedPreviewSize.width).append('x')
                    .append(supportedPreviewSize.height).append(' ');
        }
        Log.i(TAG, "Supported preview sizes: " + previewSizesString);
    }

    double screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;

    // Remove sizes that are unsuitable
    Iterator<Size> it = supportedPreviewSizes.iterator();
    while (it.hasNext()) {
        Size supportedPreviewSize = it.next();
        int realWidth = supportedPreviewSize.width;
        int realHeight = supportedPreviewSize.height;
        if (realWidth * realHeight < MIN_PREVIEW_PIXELS) {
            it.remove();
            continue;
        }

        boolean isCandidatePortrait = realWidth < realHeight;
        int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth;
        int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight;
        double aspectRatio = (double) maybeFlippedWidth / (double) maybeFlippedHeight;
        double distortion = Math.abs(aspectRatio - screenAspectRatio);
        if (distortion > MAX_ASPECT_DISTORTION) {
            it.remove();
            continue;
        }

        if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {
            Point exactPoint = new Point(realWidth, realHeight);
            Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);
            return exactPoint;
        }
    }

    // If no exact match, use largest preview size. This was not a great idea on older devices because
    // of the additional computation needed. We're likely to get here on newer Android 4+ devices, where
    // the CPU is much more powerful.
    if (!supportedPreviewSizes.isEmpty()) {
        Size largestPreview = supportedPreviewSizes.get(0);
        Point largestSize = new Point(largestPreview.width, largestPreview.height);
        Log.i(TAG, "Using largest suitable preview size: " + largestSize);
        return largestSize;
    }

    // If there is nothing at all suitable, return current preview size
    Size defaultPreview = parameters.getPreviewSize();
    if (defaultPreview == null) {
        throw new IllegalStateException("Parameters contained no preview size!");
    }
    Point defaultSize = new Point(defaultPreview.width, defaultPreview.height);
    Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize);
    return defaultSize;
}

From source file:Main.java

public static boolean handleMmi(Context context, String dialString, PhoneAccountHandle handle) {
    if (hasModifyPhoneStatePermission(context)) {
        try {//from   w w  w.  j av a2  s .c om
            if (handle == null) {
                return getTelecomManager(context).handleMmi(dialString);
            } else {
                return getTelecomManager(context).handleMmi(dialString, handle);
            }
        } catch (SecurityException e) {
            Log.w(TAG, "TelecomManager.handleMmi called without permission.");
        }
    }
    return false;
}

From source file:Main.java

/**
 * Checks if the Media Type is a DRM Media Type
 *
 * @param drmManagerClient A DrmManagerClient
 * @param mimetype Media Type to check//from ww w .j  a va  2  s .c  om
 * @return True if the Media Type is DRM else false
 */
public static boolean isDrmMimeType(Context context, String mimetype) {
    boolean result = false;
    if (context != null) {
        try {
            DrmManagerClient drmClient = new DrmManagerClient(context);
            if (drmClient != null && mimetype != null && mimetype.length() > 0) {
                result = drmClient.canHandle("", mimetype);
            }
        } catch (IllegalArgumentException e) {
            Log.w(TAG, "DrmManagerClient instance could not be created, context is Illegal.");
        } catch (IllegalStateException e) {
            Log.w(TAG, "DrmManagerClient didn't initialize properly.");
        }
    }
    return result;
}

From source file:org.thoughtcrime.securesms.mms.MmsSendHelper.java

private static byte[] makePost(MmsConnectionParameters parameters, byte[] mms)
        throws ClientProtocolException, IOException {
    Log.w("MmsSender", "Sending MMS1 of length: " + mms.length);
    try {//from w ww .j a  v a  2 s  .  c o  m
        HttpClient client = constructHttpClient(parameters);
        URI hostUrl = new URI(parameters.getMmsc());
        HttpHost target = new HttpHost(hostUrl.getHost(), hostUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
        HttpPost request = new HttpPost(parameters.getMmsc());
        ByteArrayEntity entity = new ByteArrayEntity(mms);

        entity.setContentType("application/vnd.wap.mms-message");

        request.setEntity(entity);
        request.setParams(client.getParams());
        request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");

        HttpResponse response = client.execute(target, request);
        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != 200)
            throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());

        return parseResponse(response.getEntity());
    } catch (URISyntaxException use) {
        Log.w("MmsDownlader", use);
        throw new IOException("Bad URI syntax");
    }
}

From source file:Main.java

/**
 * Write a string value to the specified file.
 * // www.  j  av  a  2 s .com
 * @param filename The filename
 * @param value The value
 */
public static void writeValue(String filename, String value) {
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(new File(filename), false);
        fos.write(value.getBytes());
        fos.flush();
        // fos.getFD().sync();
    } catch (FileNotFoundException ex) {
        Log.w(TAG, "file " + filename + " not found: " + ex);
    } catch (SyncFailedException ex) {
        Log.w(TAG, "file " + filename + " sync failed: " + ex);
    } catch (IOException ex) {
        Log.w(TAG, "IOException trying to sync " + filename + ": " + ex);
    } catch (RuntimeException ex) {
        Log.w(TAG, "exception while syncing file: ", ex);
    } finally {
        if (fos != null) {
            try {
                Log.w(TAG_WRITE, "file " + filename + ": " + value);
                fos.close();
            } catch (IOException ex) {
                Log.w(TAG, "IOException while closing synced file: ", ex);
            } catch (RuntimeException ex) {
                Log.w(TAG, "exception while closing file: ", ex);
            }
        }
    }

}

From source file:com.ammobyte.radioreddit.api.InternetCommunication.java

public static InputStream retrieveStream(String url) {

    DefaultHttpClient client = new DefaultHttpClient();

    HttpGet getRequest = new HttpGet(url);

    try {//ww  w  .  j a va2 s .  c om

        HttpResponse getResponse = client.execute(getRequest);
        final int statusCode = getResponse.getStatusLine().getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {
            Log.w("InternetConnection", "Error " + statusCode + " for URL " + url);
            return null;
        }

        HttpEntity getResponseEntity = getResponse.getEntity();
        return getResponseEntity.getContent();

    } catch (IOException e) {
        getRequest.abort();
        Log.w("InternetConnection", "Error for URL " + url, e);
    }

    return null;

}

From source file:Main.java

public static Bitmap decodeByteArray(byte[] bytes, int offset, int length, BitmapFactory.Options options) {
    if (bytes.length <= 0) {
        throw new IllegalArgumentException("bytes.length " + bytes.length + " must be a positive number");
    }//from   w ww  .j  a v  a2  s  . c o  m

    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeByteArray(bytes, offset, length, options);
    } catch (OutOfMemoryError e) {
        Log.e(LOGTAG, "decodeByteArray(bytes.length=" + bytes.length + ", options= " + options + ") OOM!", e);
        return null;
    }

    if (bitmap == null) {
        Log.w(LOGTAG, "decodeByteArray() returning null because BitmapFactory returned null");
        return null;
    }

    if (bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
        Log.w(LOGTAG, "decodeByteArray() returning null because BitmapFactory returned "
                + "a bitmap with dimensions " + bitmap.getWidth() + "x" + bitmap.getHeight());
        return null;
    }

    return bitmap;
}

From source file:Main.java

/**
 * Load a raw string resource.//from w w w  . j  a v  a 2 s.  c  o  m
 * @param context The current context.
 * @param resourceId The resource id.
 * @return The loaded string.
 */
private static String getStringFromRawResource(Context context, int resourceId) {
    String result = null;

    InputStream is = context.getResources().openRawResource(resourceId);
    if (is != null) {
        StringBuilder sb = new StringBuilder();
        String line;

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } catch (IOException e) {
            Log.w("ApplicationUtils",
                    String.format("Unable to load resource %s: %s", resourceId, e.getMessage()));
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                Log.w("ApplicationUtils",
                        String.format("Unable to load resource %s: %s", resourceId, e.getMessage()));
            }
        }
        result = sb.toString();
    } else {
        result = "";
    }

    return result;
}

From source file:Main.java

/**
 * Load a raw string resource.//from w w  w  .j av  a 2  s .  com
 * @param context The current context.
 * @param resourceId The resource id.
 * @return The loaded string.
 */
public static String getStringFromRawResource(Context context, int resourceId) {
    String result = null;

    InputStream is = context.getResources().openRawResource(resourceId);
    if (is != null) {
        StringBuilder sb = new StringBuilder();
        String line;

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } catch (IOException e) {
            Log.w("ApplicationUtils",
                    String.format("Unable to load resource %s: %s", resourceId, e.getMessage()));
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                Log.w("ApplicationUtils",
                        String.format("Unable to load resource %s: %s", resourceId, e.getMessage()));
            }
        }
        result = sb.toString();
    } else {
        result = "";
    }

    return result;
}