<commit_before>/**
 * Copyright (C) 2016 mplacona.
 *
 * 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 uk.co.placona.selfie;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;


/**
 * Class to simplify the taking screenshots of your apps
 */
public final class Selfie {

    // Storage Permissions variables
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private static final String TAG = "Selfie";
    private static SelfieInstance mInstance;

    public static void initialize(){
        if (mInstance == null) {
            mInstance = new SelfieInstance
                    .SelfieBuilder()
                    .format("yyyy-MM-dd_hh:mm:ss")
                    .path(Environment.getExternalStorageDirectory().toString())
                    .quality(100)
                    .build();
        }
    }

    private static void verifyStoragePermissions(Activity activity) {
        // Check if we have read or write permission
        int writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (writePermission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }

//    @Override
//    public void onRequestPermissionsResult(int requestCode,
//                                           String permissions[], int[] grantResults) {
//        switch (requestCode) {
//            case REQUEST_EXTERNAL_STORAGE: {
//                // If request is cancelled, the result arrays are empty.
//                if (grantResults.length > 0
//                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//
//                    // permission was granted, yay!
//                    takeScreenShot(mActivity);
//
//                } else {
//
//                    // permission denied, boo! Disable the
//                    // functionality that depends on this permission.
//                }
//                return;
//            }
//
//            // other 'case' lines to check for other
//            // permissions this app might request
//        }
//    }

    /**
     * Create a snapshot
     *
     * @param activity Activity used by Selfie.
     */
    public static void snap(Activity activity) {
        verifyStoragePermissions(activity);
        takeScreenShot(activity);
    }

    private static void takeScreenShot(Activity activity) {
        Date now = new Date();
        android.text.format.DateFormat.format(mInstance.getFormat(), now);

        try {
            // image naming and path  to include sd card  appending name you choose for file
            String mPath = mInstance.getPath() + "/" + now + ".jpg";

            // create bitmap screen capture
            View v1 = activity.getWindow().getDecorView().getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            File imageFile = new File(mPath);

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = mInstance.getQuality();
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();

            Log.d(TAG, "snap: " + mPath);


        } catch (Throwable e) {
            // Several error may come out with file handling or OOM
            e.printStackTrace();
        }
    }
}
<commit_msg>Change authentication dance<commit_after>/**
 * Copyright (C) 2016 mplacona.
 *
 * 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 uk.co.placona.selfie;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;


/**
 * Class to simplify the taking screenshots of your apps
 */
public final class Selfie {

    // Storage Permissions variables
    private static final int REQUEST_WRITE_STORAGE = 112;

    private static final String TAG = "Selfie";
    private static SelfieInstance mInstance;

    public static void initialize(){
        if (mInstance == null) {
            mInstance = new SelfieInstance
                    .SelfieBuilder()
                    .format("yyyy-MM-dd_hh:mm:ss")
                    .path(Environment.getExternalStorageDirectory().toString())
                    .quality(100)
                    .build();
        }
    }

    /**
     * Create a snapshot
     *
     * @param activity Activity used by Selfie.
     */
    public static void snap(Activity activity) {

        boolean hasPermission = (ContextCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
        if (!hasPermission) {
            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_WRITE_STORAGE);
        }else {
            takeScreenShot(activity);
        }
    }

    private static void takeScreenShot(Activity activity) {
        Date now = new Date();
        android.text.format.DateFormat.format(mInstance.getFormat(), now);

        try {
            // image naming and path  to include sd card  appending name you choose for file
            String mPath = mInstance.getPath() + "/" + now + ".jpg";

            // create bitmap screen capture
            View v1 = activity.getWindow().getDecorView().getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            File imageFile = new File(mPath);

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = mInstance.getQuality();
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();

            Log.d(TAG, "snap: " + mPath);


        } catch (Throwable e) {
            // Several error may come out with file handling or OOM
            e.printStackTrace();
        }
    }
}

{'fraction_non_alphanumeric': 0.09101040118870728, 'fraction_numerical': 0.0048291233283803865, 'mean_word_length': 2.1962801741195093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4436692', 'n_tokens_mistral': 2095, 'n_tokens_neox': 2007, 'n_words': 603}