Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 *  Copyright 2012-2014 Quantcast Corp.
 *
 * This software is licensed under the Quantcast Mobile App Measurement Terms of Service
 * https://www.quantcast.com/learning-center/quantcast-terms/mobile-app-measurement-tos
 * (the License?). You may not use this file unless (1) you sign up for an account at
 * https://www.quantcast.com and click your agreement to the License and (2) are in
 * compliance with the License. See the License for the specific language governing
 * permissions and limitations under the License. Unauthorized use of this file constitutes
 * copyright infringement and violation of law.
 */

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    private static final String QCMEASUREMENT_OPTOUT_STRING = "QC-OPT-OUT";

    static boolean isOptedOut(Context appContext) {
        return isOptedOut(appContext, true);
    }

    private static boolean isOptedOut(Context appContext, boolean shouldAsk) {
        boolean optedOut = false;
        FileInputStream in = null;
        try {
            in = appContext.openFileInput(QCMEASUREMENT_OPTOUT_STRING);
            optedOut = in.read() != 0;
        } catch (FileNotFoundException e) {
            if (shouldAsk)
                askEveryone(appContext, false, false);
        } catch (IOException ignored) {
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ignored) {
            }
        }

        return optedOut;

    }

    static void askEveryone(final Context context, boolean optedOut, boolean shouldUpdate) {
        Boolean status = false;
        PackageManager pm = context.getPackageManager();
        if (pm != null) {
            for (PackageInfo info : pm.getInstalledPackages(0)) {
                if (!info.packageName.equals(context.getPackageName())) {
                    try {
                        Context foreignContext = context.createPackageContext(info.packageName, 0);
                        if (shouldUpdate) {
                            if (isQuantified(foreignContext)) {
                                createOptOut(foreignContext, optedOut);
                            }
                        } else {
                            status = isOptedOut(foreignContext, false);
                            if (status) {
                                break;
                            }
                        }
                    } catch (Exception ignored) {
                    }
                }
            }
        }
        if (!shouldUpdate) {
            createOptOut(context, status);
        }
    }

    static boolean isQuantified(Context appContext) {
        File quantified = appContext.getFileStreamPath(QCMEASUREMENT_OPTOUT_STRING);
        return quantified != null && quantified.exists();
    }

    static void createOptOut(Context context, boolean optedOut) {
        FileOutputStream stream = null;
        try {
            stream = context.openFileOutput(QCMEASUREMENT_OPTOUT_STRING,
                    Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
            stream.write(optedOut ? 1 : 0);
        } catch (Exception ignored) {
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (IOException ignored) {
            }
        }
    }
}