<repo_name>devalticode/FlashAlert<file_sep>/app/src/main/java/com/alticode/flashalert/utils/FlashPreferenceCategory.java
package com.alticode.flashalert.utils;

import android.content.Context;
import android.graphics.Color;
import android.preference.PreferenceCategory;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;


/**
 * Created by TienDzung on 9/6/2015.
 */
public class FlashPreferenceCategory extends PreferenceCategory {
    public FlashPreferenceCategory(Context context) {
        super(context);
    }

    public FlashPreferenceCategory(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FlashPreferenceCategory(Context context, AttributeSet attrs,
                                   int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        TextView titleView = (TextView) view.findViewById(android.R.id.title);
        titleView.setTextColor(Color.parseColor("#30a399"));
    }
}<file_sep>/app/src/main/java/com/alticode/flashalert/flashalert/FlashApplication.java
package com.alticode.flashalert.flashalert;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;

import com.alticode.flashalert.R;
import com.alticode.flashalert.utils.FlashHelper;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Dzung on 9/18/2015.
 */
public class FlashApplication extends Application {
    public static SharedPreferences sharePref;
    public static Context mContext;
    public static FlashHelper mFlashHelper;
    public BatteryReceiver batteryReceiver;

    private static final int NOTIFY_TIMER = 60000 * 60;
    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        sharePref = PreferenceManager.getDefaultSharedPreferences(this);
        mFlashHelper = FlashHelper.getInstance(this);
        /* Register battery receiver */
        batteryReceiver = new BatteryReceiver();
        IntentFilter iFilter = new IntentFilter();
        iFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryReceiver, iFilter);

        /* Create notify */
        Timer notifyTimer = new Timer();
        TimerTask notifyTask = new TimerTask() {
            @Override
            public void run() {
                createNotify();
            }
        };
        notifyTimer.scheduleAtFixedRate(notifyTask, 0, NOTIFY_TIMER);
    }

    public static boolean getPrefIncomingCall() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.status_call_pref), false);
    }

    public static boolean getPrefIncomingSms() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.status_sms_pref), false);
    }

    public static boolean getPrefModeSilent() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.mode_silent_pref), false);
    }

    public static boolean getPrefModeVibrate() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.mode_vibrate_pref), false);
    }

    public static boolean getPrefModeNormal() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.mode_normal_pref), false);
    }

    public static int getPrefSpeed() {
        return sharePref.getInt(mContext.getResources().getString(R.string.speed_pref), 100);
    }

    public static int getPrefBlinkTimes() {
        return sharePref.getInt(mContext.getResources().getString(R.string.blink_time_pref), 3);
    }

    public static int getPrefBattery() {
        return sharePref.getInt(mContext.getResources().getString(R.string.battery_pref), 20);
    }

    public static boolean getPrefNotify() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.notify_pref), false);
    }

    public static boolean getPrefAlert() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.alert_key_pref), true);
    }

    public static void savePrefAlert(boolean value) {
        sharePref.edit().putBoolean(mContext.getResources().getString(R.string.alert_key_pref), value).apply();
    }

    private void createNotify() {
        Intent i = new Intent(this, SettingsActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this.getApplicationContext(), (int) System.currentTimeMillis(), i, 0);
        Notification notify;
        Notification.Builder notifyBuilder = new Notification.Builder(this)
                .setContentTitle(getString(R.string.app_name))
                .setContentText("Touch to more setting")
                .setSmallIcon(R.drawable.ic_flash_on_white_36dp)
                .setContentIntent(pIntent);
        // Build notification
        if (Build.VERSION.SDK_INT < 16) {
            notify = notifyBuilder.getNotification();
        } else {
            notify = notifyBuilder.build();
        }

        NotificationManager notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notify.flags |= Notification.FLAG_AUTO_CANCEL;
        notifyManager.notify(0, notify);
    }
}
<file_sep>/app/src/main/java/com/alticode/flashalert/flashalert/SMSreceiver.java
package com.alticode.flashalert.flashalert;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alticode.flashalert.utils.FlashHelper;


public class SMSreceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        FlashHelper mFlashHelper = FlashApplication.mFlashHelper;
        Bundle extras = intent.getExtras();
        if (extras != null) {
            if ((mFlashHelper.batteryLevel >= FlashApplication.getPrefBattery()) && mFlashHelper.isAlertOn() == true && FlashApplication.getPrefIncomingSms())
                mFlashHelper.alert(FlashHelper.INCOMING_SMS);
        }
    }

}<file_sep>/app/src/main/java/com/alticode/flashalert/utils/PrefHelper.java
package com.alticode.flashalert.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.preference.PreferenceManager;

import com.alticode.flashalert.R;


/**
 * Created by Dzung on 9/14/2015.
 */
public class PrefHelper {
    private Context mContext;
    private SharedPreferences sharePref;
    private static PrefHelper mInstance = null;

    private PrefHelper(Context context) {
        mContext = context;
        sharePref = PreferenceManager.getDefaultSharedPreferences(mContext);
    }

    public static PrefHelper getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new PrefHelper(context);
        }
        return mInstance;
    }

    public boolean getPrefIncomingCall() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.status_call_pref), false);
    }

    public boolean getPrefIncomingSms() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.status_sms_pref), false);
    }

    public boolean getPrefModeSilent() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.mode_silent_pref), false);
    }

    public boolean getPrefModeVibrate() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.mode_vibrate_pref), false);
    }

    public boolean getPrefModeNormal() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.mode_normal_pref), false);
    }

    public int getPrefSpeed() {
        return sharePref.getInt(mContext.getResources().getString(R.string.speed_pref), 100);
    }

    public int getPrefBlinkTimes() {
        return sharePref.getInt(mContext.getResources().getString(R.string.blink_time_pref), 3);
    }

    public int getPrefBattery() {
        return sharePref.getInt(mContext.getResources().getString(R.string.battery_pref), 20);
    }

    public boolean getPrefNotify() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.notify_pref), false);
    }

    public boolean getPrefAlert() {
        return sharePref.getBoolean(mContext.getResources().getString(R.string.alert_key_pref), true);
    }

    public void savePrefAlert(boolean value) {
        sharePref.edit().putBoolean(mContext.getResources().getString(R.string.alert_key_pref), value).apply();
    }
}
<file_sep>/app/src/main/java/com/alticode/flashalert/utils/Config.java
package com.alticode.flashalert.utils;

import android.util.Log;

import java.util.Random;

/**
 * Created by TienDzung on 9/14/2015.
 */
public class Config {
    public static final int AD_FREQUENCY = 3;
    public static final boolean SHOW_FULL_AD_AT_CREATE = true;
    public static final boolean SHOW_FULL_AD_AT_CALL = true;
    public static final boolean SHOW_FULL_AD_AT_SMS = true;
    public static final boolean SHOW_FULL_AD_AT_SILENT = true;
    public static final boolean SHOW_FULL_AD_AT_VIBRATE = true;
    public static final boolean SHOW_FULL_AD_AT_NORMAL = true;
    public static final boolean SHOW_BANNER_AD = true;

    public static final String DEVICE_TEST_SONY = "YT91008UDL";
    public static final String DEVICE_TEST_LG = "LGE975d3097707";

    public static final String APP_URL = "https://play.google.com/store/apps/details?id=alert.flash.com.flashalert \n\n";
    public static final String RATE_APP_URL = "https://play.google.com/store/apps/details?id=alert.flash.com.flashalert";
    public static final String MORE_APP_URL = "https://play.google.com/store/apps/details?id=alert.flash.com.flashalert";

    public static int randomInt() {
        Random rand = new Random();
        return rand.nextInt();
    }

    public static boolean allowAd() {
        boolean show = randomInt() % AD_FREQUENCY == 0 ? true : false;
        Log.d("DungNT", "Show: " + show);
        return show;
    }
}
<file_sep>/app/src/main/java/com/alticode/flashalert/flashalert/CallReceiver.java
package com.alticode.flashalert.flashalert;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.alticode.flashalert.utils.FlashHelper;

/**
 * Created by TienDzung on 9/7/2015.
 */
public class CallReceiver extends BroadcastReceiver {
    FlashApplication app;
    @Override
    public void onReceive(Context context, Intent intent) {
        app = (FlashApplication)context.getApplicationContext();
        FlashHelper mFlashHelper = FlashApplication.mFlashHelper;

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
               if ((mFlashHelper.batteryLevel >= FlashApplication.getPrefBattery()) && mFlashHelper.isAlertOn() == true && FlashApplication.getPrefIncomingCall() == true) {
                mFlashHelper.stop = false;
                mFlashHelper.alert(FlashHelper.INCOMING_CALL);
            }
        } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_IDLE)
                || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            mFlashHelper.stop = true;
        }
    }
}
