<commit_before>/*
 * Copyright 2013 <NAME> (@f2prateek)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.f2prateek.dfg.core;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import com.crashlytics.android.Crashlytics;
import com.f2prateek.dfg.AppConstants;
import com.f2prateek.dfg.R;
import com.f2prateek.dfg.model.Device;
import com.f2prateek.dfg.util.BitmapUtils;
import com.f2prateek.dfg.util.Ln;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DeviceFrameGenerator {

  private final Context context;
  private final Callback callback;
  private final Device device;
  private final boolean withShadow;
  private final boolean withGlare;

  public DeviceFrameGenerator(Context context, Callback callback, Device device, boolean withShadow,
      boolean withGlare) {
    this.context = context;
    this.callback = callback;
    this.device = device;
    this.withShadow = withShadow;
    this.withGlare = withGlare;
  }

  /**
   * Checks if screenshot matches the aspect ratio of the device.
   *
   * @param device The Device to frame.
   * @param screenshot The screenshot to frame.
   * @return "port" if matched to portrait and "land" if matched to landscape
   * @throws UnmatchedDimensionsException If it could not match any orientation to the device.
   */
  private static String checkDimensions(Device device, Bitmap screenshot)
      throws UnmatchedDimensionsException {
    float aspect1 = (float) screenshot.getHeight() / (float) screenshot.getWidth();
    float aspect2 = (float) device.getPortSize()[1] / (float) device.getPortSize()[0];

    if (aspect1 == aspect2) {
      return "port";
    } else if (aspect1 == 1 / aspect2) {
      return "land";
    }

    Ln.e("Screenshot height=%d, width=%d. Device height=%d, width=%d. Aspect1=%f, Aspect2=%f",
        screenshot.getHeight(), screenshot.getWidth(), device.getPortSize()[1],
        device.getPortSize()[0], aspect1, aspect2);
    throw new UnmatchedDimensionsException(device, screenshot.getHeight(), screenshot.getWidth());
  }

  /**
   * Check if the orientation is portrait
   *
   * @param orientation Orientation to check.
   * @return true if orientation is portrait
   */
  private static boolean isPortrait(String orientation) {
    return (orientation.compareTo("port") == 0);
  }

  /**
   * Generate the frame.
   *
   * @param screenshotUri Uri to the screenshot file.
   */
  public void generateFrame(Uri screenshotUri) {
    Ln.d("Generating for %s %s and %s from file %s.", device.getName(),
        withGlare ? " with glare " : " without glare ",
        withShadow ? " with shadow " : " without shadow ", screenshotUri);

    final Bitmap screenshot;
    try {
      screenshot = BitmapUtils.decodeUri(context.getContentResolver(), screenshotUri);
    } catch (IOException e) {
      Resources r = context.getResources();
      callback.failedImage(r.getString(R.string.failed_open_screenshot_title),
          r.getString(R.string.failed_open_screenshot_text),
          r.getString(R.string.failed_open_screenshot_text, screenshotUri.toString()));
      Crashlytics.logException(e);
      return;
    }
    generateFrame(screenshot);
  }

  /**
   * Generate the frame.
   *
   * @param screenshot Screenshot to use.
   */
  private void generateFrame(Bitmap screenshot) {
    callback.startingImage(screenshot);
    String orientation;
    try {
      orientation = checkDimensions(device, screenshot);
    } catch (UnmatchedDimensionsException e) {
      Ln.e(e);
      Resources r = context.getResources();
      String failed_title = r.getString(R.string.failed_match_dimensions_title);
      String failed_text =
          r.getString(R.string.failed_match_dimensions_text, e.device.getPortSize()[0],
              e.device.getPortSize()[1], e.screenshotHeight, e.screenshotWidth);
      String failed_small_text = r.getString(R.string.device_chosen, device.getName());
      callback.failedImage(failed_title, failed_small_text, failed_text);
      return;
    }

    final Bitmap background =
        BitmapUtils.decodeResource(context, device.getBackgroundString(orientation));<commit_msg>Format a string from resource, convert screenshot to local<commit_after>/*
 * Copyright 2013 <NAME> (@f2prateek)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.f2prateek.dfg.core;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import com.crashlytics.android.Crashlytics;
import com.f2prateek.dfg.AppConstants;
import com.f2prateek.dfg.R;
import com.f2prateek.dfg.model.Device;
import com.f2prateek.dfg.util.BitmapUtils;
import com.f2prateek.dfg.util.Ln;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DeviceFrameGenerator {

  private final Context context;
  private final Callback callback;
  private final Device device;
  private final boolean withShadow;
  private final boolean withGlare;

  public DeviceFrameGenerator(Context context, Callback callback, Device device, boolean withShadow,
      boolean withGlare) {
    this.context = context;
    this.callback = callback;
    this.device = device;
    this.withShadow = withShadow;
    this.withGlare = withGlare;
  }

  /**
   * Checks if screenshot matches the aspect ratio of the device.
   *
   * @param device The Device to frame.
   * @param screenshot The screenshot to frame.
   * @return "port" if matched to portrait and "land" if matched to landscape
   * @throws UnmatchedDimensionsException If it could not match any orientation to the device.
   */
  private static String checkDimensions(Device device, Bitmap screenshot)
      throws UnmatchedDimensionsException {
    float aspect1 = (float) screenshot.getHeight() / (float) screenshot.getWidth();
    float aspect2 = (float) device.getPortSize()[1] / (float) device.getPortSize()[0];

    if (aspect1 == aspect2) {
      return "port";
    } else if (aspect1 == 1 / aspect2) {
      return "land";
    }

    Ln.e("Screenshot height=%d, width=%d. Device height=%d, width=%d. Aspect1=%f, Aspect2=%f",
        screenshot.getHeight(), screenshot.getWidth(), device.getPortSize()[1],
        device.getPortSize()[0], aspect1, aspect2);
    throw new UnmatchedDimensionsException(device, screenshot.getHeight(), screenshot.getWidth());
  }

  /**
   * Check if the orientation is portrait
   *
   * @param orientation Orientation to check.
   * @return true if orientation is portrait
   */
  private static boolean isPortrait(String orientation) {
    return (orientation.compareTo("port") == 0);
  }

  /**
   * Generate the frame.
   *
   * @param screenshotUri Uri to the screenshot file.
   */
  public void generateFrame(Uri screenshotUri) {
    Ln.d("Generating for %s %s and %s from file %s.", device.getName(),
        withGlare ? " with glare " : " without glare ",
        withShadow ? " with shadow " : " without shadow ", screenshotUri);

    try {
      Bitmap screenshot = BitmapUtils.decodeUri(context.getContentResolver(), screenshotUri);
      generateFrame(screenshot);
    } catch (IOException e) {
      Resources r = context.getResources();
      callback.failedImage(r.getString(R.string.failed_open_screenshot_title),
          r.getString(R.string.failed_open_screenshot_text, screenshotUri.toString()),
          r.getString(R.string.failed_open_screenshot_text, screenshotUri.toString()));
      Crashlytics.logException(e);
    }
  }

  /**
   * Generate the frame.
   *
   * @param screenshot Screenshot to use.
   */
  private void generateFrame(Bitmap screenshot) {
    callback.startingImage(screenshot);
    String orientation;
    try {
      orientation = checkDimensions(device, screenshot);
    } catch (UnmatchedDimensionsException e) {
      Ln.e(e);
      Resources r = context.getResources();
      String failed_title = r.getString(R.string.failed_match_dimensions_title);
      String failed_text =
          r.getString(R.string.failed_match_dimensions_text, e.device.getPortSize()[0],
              e.device.getPortSize()[1], e.screenshotHeight, e.screenshotWidth);
      String failed_small_text = r.getString(R.string.device_chosen, device.getName());
      callback.failedImage(failed_title, failed_small_text, failed_text);
      return;
    }

    final Bitmap background =
        BitmapUtils.decodeResource(context, device.getBackgroundString(orientation));
{'fraction_non_alphanumeric': 0.10188981101889812, 'fraction_numerical': 0.0065993400659934, 'mean_word_length': 3.781070745697897, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6960938', 'n_tokens_mistral': 2929, 'n_tokens_neox': 2825, 'n_words': 797}