com.hackeruproj.android.havatzfit.general_utilities.GenUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.hackeruproj.android.havatzfit.general_utilities.GenUtils.java

Source

package com.hackeruproj.android.havatzfit.general_utilities;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.hackeruproj.android.havatzfit.R;

import java.util.Calendar;

/**
 * Created by Igor on 10-Aug-16.
 */

//general utilities class to hold all general values and methods of the App
public class GenUtils {
    //final strings for bundle get arguments key
    public static final String PROGRAM = "program";
    public static final String MODE = "mode";

    //license agreement of the user
    public static final String HAS_AGREED = "termsAgreed";

    //final string for the SP XML file name
    public static final String STAY_ON = "saveLogIn";
    public static final String IS_CHECKED = "isChecked";
    //creating variables for the Context and the prefs

    //start sharedprefrences values
    private Context context;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    //finish sharedprefrences values

    public GenUtils(Context context) {
        this.context = context;
    }

    //start method that sets the shared prefrences and itseditor
    private void setSharedPrefs() {
        pref = context.getApplicationContext().getSharedPreferences(STAY_ON, 0);
        editor = pref.edit();

    }
    //finish method that sets the shared prefrences and itseditor

    //start methods for SharedPrefs
    public void saveUser(String value) {
        setSharedPrefs();
        editor.putBoolean(value, true).commit();
    }

    public boolean isChecked(String value) {
        setSharedPrefs();
        boolean checked = pref.getBoolean(value, false);

        return checked;
    }

    public void removeUser(String value) {
        setSharedPrefs();
        editor.remove(value).commit();
    }
    //finish methods for SharedPrefs

    //start check runtime permission
    public static boolean chkLocPerm(Context context) {
        if (ActivityCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(context,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }

        return true;
    }
    //finish check runtime permission  

    //start get current date method
    public static String getCurrentDate() {
        int day, month, year; //<-- int variables needed to draw from calendar class
        String signUpDate; //<-- String variable to compose the date of the user signing up for the first time!

        //start Calendar init
        Calendar c = Calendar.getInstance();
        //finish Calendar init

        //start insert current date into vars
        day = c.get(Calendar.DATE);
        month = c.get(Calendar.MONTH) + 1;//<-- set the month count from 1 not 0
        year = c.get(Calendar.YEAR);
        //finish insert current date into vars

        //start compose String holding the date of First Signup!
        signUpDate = "Day: " + day + " ";
        signUpDate += "Month: " + month + " ";
        signUpDate += "Year: " + year;
        //finish start compose String holding the date of First Signup!

        return signUpDate;
    }
    //finish get currrent date method

    //start static method to convert strings into btimap
    public static Bitmap string2Bitmap(String encodeString) {
        try {
            byte[] encodeByte = Base64.decode(encodeString, Base64.DEFAULT);
            Bitmap mBitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
            return mBitmap;
        } catch (Exception e) {
            e.getMessage();
            return null;
        }
    }
    //finish static method to convert strings into btimap

    //start static bundle method
    public static Bundle setStringArgsBundle(String bundleKey, String bundleValue) {
        Bundle bundleArgs = new Bundle();
        bundleArgs.putString(bundleKey, bundleValue);

        return bundleArgs;
    }
    //finish static bundle method

    //start static bundle overloaded method set 2 args
    public static Bundle setStringArgsBundle(String bundleKey, String bundleValue, String bundleKey2,
            String bundleValue2) {
        Bundle bundleArgs = new Bundle();
        bundleArgs.putString(bundleKey, bundleValue);
        bundleArgs.putString(bundleKey2, bundleValue2);

        return bundleArgs;
    }
    //finish static bundle overloaded method set 2 args

    //start test connection
    public static boolean isNetworkEnabled(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwrok = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwrok != null && activeNetwrok.isConnectedOrConnecting();
        return isConnected;
    }
    //finish test connection

    //start
    public static void menuAlerts(Context context, int header, int theme, int img) {
        AlertDialog.Builder menuBuilder = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View menuView = inflater.inflate(R.layout.inflater_alertdialog, null);
        menuBuilder.setView(menuView);

        TextView headerText = (TextView) menuView.findViewById(R.id.alertTxtHeader);
        TextView bodyText = (TextView) menuView.findViewById(R.id.alertTxtBody);
        ImageView imgView = (ImageView) menuView.findViewById(R.id.alertImgBody);

        headerText.setText(header);
        bodyText.setText(theme);
        imgView.setImageResource(img);
        menuBuilder.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        menuBuilder.show();
    }
    //finish
}