com.sentaroh.android.SMBSync2.GlobalParameters.java Source code

Java tutorial

Introduction

Here is the source code for com.sentaroh.android.SMBSync2.GlobalParameters.java

Source

package com.sentaroh.android.SMBSync2;

/*
The MIT License (MIT)
Copyright (c) 2011-2013 Sentaroh
    
Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to 
the following conditions:
    
The above copyright notice and this permission notice shall be included in all copies or 
substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
    
*/

import static com.sentaroh.android.SMBSync2.Constants.*;
import static com.sentaroh.android.SMBSync2.Log.LogConstants.*;

import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;

import com.sentaroh.android.SMBSync2.R;
import com.sentaroh.android.Utilities.CommonGlobalParms;
import com.sentaroh.android.Utilities.LocalMountPoint;
import com.sentaroh.android.Utilities.SafUtil;
import com.sentaroh.android.Utilities.ThemeColorList;
import com.sentaroh.android.Utilities.ThreadCtrl;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.PowerManager.WakeLock;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.support.v4.app.NotificationCompat.Builder;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class GlobalParameters extends CommonGlobalParms {
    public Context appContext = null;

    public boolean externalStorageIsMounted = false;
    public boolean externalStorageAccessIsPermitted = false;
    public String internalRootDirectory = "/";
    public final static String UNKNOWN_USB_DIRECTORY = LocalMountPoint.UNKNOWN_USB_DIRECTORY;
    public String usbRootDirectory = UNKNOWN_USB_DIRECTORY;
    public String applicationRootDirectory = "/";
    @SuppressLint("SdCardPath")
    public final static String UNKNOWN_SDCARD_DIRECTORY = "/sdcard_unknown";
    public String sdcardRootDirectory = UNKNOWN_SDCARD_DIRECTORY;

    public String profilePassword = "";
    public final String profileKeyPrefix = "*SMBSync2*";

    public ArrayList<SyncTaskItem> syncTaskList = null;

    public ArrayBlockingQueue<SyncRequestItem> syncRequestQueue = new ArrayBlockingQueue<SyncRequestItem>(1000);

    public ThreadCtrl syncThreadConfirm = new ThreadCtrl();
    public ThreadCtrl syncThreadControl = new ThreadCtrl();

    public boolean syncThreadEnabled = true;
    public boolean syncThreadActive = false;
    public String syncThreadRequestID = "";

    public ISvcCallback callbackStub = null;

    //   public boolean activityIsBackgroud=false;

    public ArrayList<SyncHistoryItem> syncHistoryList = null;

    public ArrayList<MsgListItem> msgList = new ArrayList<MsgListItem>();

    public boolean freezeMessageViewScroll = false;

    public boolean sampleProfileCreateRequired = false;

    public boolean wifiIsActive = false;
    public String wifiSsid = "";

    public WakeLock dimScreenWakelock = null;
    public WifiManager.WifiLock wifiLock = null;

    public boolean themeIsLight = true;
    public int applicationTheme = -1;
    public ThemeColorList themeColorList = null;

    //   Settings parameter          
    public boolean settingExitClean;
    public int settingDebugLevel = 3;
    public boolean settingUseLightTheme = false;
    public int settingLogMaxFileCount = 10;
    public String settingLogMsgDir = "", settingLogMsgFilename = LOG_FILE_NAME;
    public boolean settingLogOption = false;

    //Parameters from Settings m
    public boolean settingErrorOption = false;
    public String settingScreenOnOption = KEEP_SCREEN_ON_WHEN_SCREEN_UNLOCKED;
    public static final String KEEP_SCREEN_ON_DISABLED = "0";
    public static final String KEEP_SCREEN_ON_ALWAYS = "1";
    public static final String KEEP_SCREEN_ON_WHEN_SCREEN_UNLOCKED = "2";
    public boolean settingWifiLockRequired = true;

    public boolean settingExportedProfileEncryptRequired = true;
    //   public boolean settingInternalProfileEncryptRequired=true;

    public static final String VIBRATE_WHEN_SYNC_ENDED_NONE = "0";
    public static final String VIBRATE_WHEN_SYNC_ENDED_ALWAYS = "1";
    public static final String VIBRATE_WHEN_SYNC_ENDED_SUCCESS = "2";
    public static final String VIBRATE_WHEN_SYNC_ENDED_ERROR = "3";
    public String settingVibrateWhenSyncEnded = VIBRATE_WHEN_SYNC_ENDED_ALWAYS;

    public static final String RINGTONE_WHEN_SYNC_ENDED_NONE = "0";
    public static final String RINGTONE_WHEN_SYNC_ENDED_ALWAYS = "1";
    public static final String RINGTONE_WHEN_SYNC_ENDED_SUCCESS = "2";
    public static final String RINGTONE_WHEN_SYNC_ENDED_ERROR = "3";
    public String settingRingtoneWhenSyncEnded = RINGTONE_WHEN_SYNC_ENDED_ALWAYS;

    public Handler uiHandler = null;

    public NotificationManager notificationManager = null;
    public int notificationSmallIcon = R.drawable.ic_48_smbsync_wait;
    public Notification notification = null;
    public Builder notificationBuilder = null;
    public BigTextStyle notificationBigTextStyle = null;
    public Intent notificationIntent = null;
    public PendingIntent notificationPendingIntent = null;
    public String notificationLastShowedMessage = null, notificationLastShowedTitle = "";
    public long notificationLastShowedWhen = 0;
    public String notificationAppName = "";
    //   public boolean notiifcationEnabled=false;
    public long notificationNextShowedTime = 0;
    public Bitmap notificationLargeIcon = null;

    public AdapterMessageList msgListAdapter = null;
    public ListView msgListView = null;

    public AdapterSyncHistory syncHistoryAdapter = null;
    public ListView syncHistoryListView = null;

    public AdapterSyncTask syncTaskAdapter = null;
    public ListView syncTaskListView = null;

    public TextView scheduleInfoView = null;
    public String scheduleInfoText = "";

    public boolean dialogWindowShowed = false;
    public String progressSpinSyncprofText = "", progressSpinMsgText = "";

    public boolean confirmDialogShowed = false;
    public String confirmDialogFilePath = "";
    public String confirmDialogMethod = "";

    public LinearLayout confirmView = null;
    public TextView confirmMsg = null;
    public Button confirmCancel = null;
    public OnClickListener confirmCancelListener = null;
    public Button confirmYes = null;
    public OnClickListener confirmYesListener = null;
    public Button confirmNo = null;
    public OnClickListener confirmNoListener = null;
    public Button confirmYesAll = null;
    public OnClickListener confirmYesAllListener = null;
    public Button confirmNoAll = null;
    public OnClickListener confirmNoAllListener = null;

    public LinearLayout progressBarView = null;
    public TextView progressBarMsg = null;
    public ProgressBar progressBarPb = null;
    public Button progressBarCancel = null;
    public OnClickListener progressBarCancelListener = null;
    public Button progressBarImmed = null;
    public OnClickListener progressBarImmedListener = null;

    public LinearLayout progressSpinView = null;
    public TextView progressSpinSyncprof = null;
    public TextView progressSpinMsg = null;
    public Button progressSpinCancel = null;
    public OnClickListener progressSpinCancelListener = null;

    public GlobalParameters() {
    };

    public void clearParms() {

    }

    @SuppressLint("NewApi")
    public void initCommon(Context c) {

        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            externalStorageIsMounted = false;
        } else {
            externalStorageIsMounted = true;
        }

        if (Build.VERSION.SDK_INT >= 23) {
            externalStorageAccessIsPermitted = (c.checkSelfPermission(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
        } else {
            externalStorageAccessIsPermitted = true;
        }

        internalRootDirectory = Environment.getExternalStorageDirectory().toString();
        usbRootDirectory = LocalMountPoint.getUsbStorageDir();
        sdcardRootDirectory = SafUtil.getSafExternalSdcardDir(c);
        applicationRootDirectory = c.getFilesDir().toString();
    }

    public void setLogParms(GlobalParameters gp) {
        setDebugLevel(gp.settingDebugLevel);
        setLogLimitSize(2 * 1024 * 1024);
        setLogMaxFileCount(gp.settingLogMaxFileCount);
        setLogEnabled(gp.settingLogOption);
        setLogDirName(gp.settingLogMsgDir);
        setLogFileName(gp.settingLogMsgFilename);
        setApplicationTag(APPLICATION_TAG);
        setLogIntent(BROADCAST_LOG_RESET, BROADCAST_LOG_DELETE, BROADCAST_LOG_FLUSH, BROADCAST_LOG_ROTATE,
                BROADCAST_LOG_SEND, BROADCAST_LOG_CLOSE);

    }

    //   private int mTextColorForeground=0;
    //   private int mTextColorBackground=0;
    //   public void initTextColor(Context c) {
    //       TypedValue outValue = new TypedValue();
    //       c.getTheme().resolveAttribute(android.R.attr.textColorPrimary, outValue, true);
    //       mTextColorForeground=c.getResources().getColor(outValue.resourceId);
    //       c.getTheme().resolveAttribute(android.R.attr.colorBackground, outValue, true);
    //       mTextColorBackground=c.getResources().getColor(outValue.resourceId);
    //       Log.v("","f="+String.format("0x%08x", mTextColorForeground));
    //       Log.v("","b="+String.format("0x%08x", mTextColorBackground));
    //   };

    public void initSettingsParms(Context c) {

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

        if (prefs.getString(c.getString(R.string.settings_log_dir), "-1").equals("-1")) {
            Editor pe = prefs.edit();

            sampleProfileCreateRequired = true;

            pe.putString(c.getString(R.string.settings_log_dir),
                    internalRootDirectory + "/" + APPLICATION_TAG + "/");

            pe.putBoolean(c.getString(R.string.settings_exit_clean), true);

            pe.putString(c.getString(R.string.settings_smb_lm_compatibility), "0");
            pe.putBoolean(c.getString(R.string.settings_smb_use_extended_security), false);
            pe.putString(c.getString(R.string.settings_smb_log_level), "0");
            pe.putString(c.getString(R.string.settings_smb_rcv_buf_size), "66576");
            pe.putString(c.getString(R.string.settings_smb_snd_buf_size), "66576");
            pe.putString(c.getString(R.string.settings_smb_listSize), "65535");
            pe.putString(c.getString(R.string.settings_smb_maxBuffers), "100");
            pe.putString(c.getString(R.string.settings_smb_tcp_nodelay), "false");
            pe.putString(c.getString(R.string.settings_io_buffers), "8");

            prefs.edit().putString(c.getString(R.string.settings_keep_screen_on),
                    KEEP_SCREEN_ON_WHEN_SCREEN_UNLOCKED);

            pe.commit();
        }

    };

    public void loadSettingsParms(Context c) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

        settingDebugLevel = Integer.parseInt(prefs.getString(c.getString(R.string.settings_log_level), "0"));
        //         Integer.parseInt(prefs.getString(getString(R.string.settings_log_level), "1"));//for debug

        settingLogMaxFileCount = Integer
                .valueOf(prefs.getString(c.getString(R.string.settings_log_file_max_count), "10"));

        settingLogMsgDir = prefs.getString(c.getString(R.string.settings_log_dir),
                internalRootDirectory + "/" + APPLICATION_TAG + "/");

        settingLogOption = prefs.getBoolean(c.getString(R.string.settings_log_option), false);
        //            prefs.getString(getString(R.string.settings_log_option), "1"); //for debug

        settingErrorOption = prefs.getBoolean(c.getString(R.string.settings_error_option), false);

        settingScreenOnOption = prefs.getString(c.getString(R.string.settings_keep_screen_on),
                KEEP_SCREEN_ON_WHEN_SCREEN_UNLOCKED);

        settingWifiLockRequired = prefs.getBoolean(c.getString(R.string.settings_wifi_lock), false);

        settingRingtoneWhenSyncEnded = prefs
                .getString(c.getString(R.string.settings_playback_ringtone_when_sync_ended), "0");

        settingVibrateWhenSyncEnded = prefs.getString(c.getString(R.string.settings_vibrate_when_sync_ended), "0");

        settingExportedProfileEncryptRequired = prefs
                .getBoolean(c.getString(R.string.settings_exported_profile_encryption), true);

        //      if (Build.VERSION.SDK_INT>=11) themeIsLight=prefs.getBoolean(c.getString(R.string.settings_use_light_theme), false);
        //      else themeIsLight=false;
        themeIsLight = settingUseLightTheme = prefs.getBoolean(c.getString(R.string.settings_use_light_theme),
                false);
        if (settingUseLightTheme) {
            applicationTheme = R.style.MainLight;
            //         dialogViewBackGroundColor=Color.argb(255, 50, 50, 50);//.BLACK;
        } else {
            applicationTheme = R.style.Main;
            //         dialogViewBackGroundColor=Color.argb(255, 50, 50, 50);//.BLACK;
        }
        //      if (Build.VERSION.SDK_INT>=21) dialogViewBackGroundColor=0xff333333;

    };

    public String settingsSmbLogLevel = "0", settingsSmbRcvBufSize = "16644", settingsSmbSndBufSize = "16644",
            settingsSmbListSize = "130170", settingsSmbMaxBuffers = "", settingsSmbTcpNodelay = "false",
            settingsSmbIoBuffers = "8", settingsSmbLmCompatibility = "0", settingsSmbUseExtendedSecurity = "true";

    final public void initJcifsOption(Context c) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
        settingsSmbLogLevel = prefs.getString(c.getString(R.string.settings_smb_log_level), "0");
        if (settingsSmbLogLevel.length() == 0)
            settingsSmbLogLevel = "0";

        settingsSmbIoBuffers = prefs.getString(c.getString(R.string.settings_io_buffers), "8");

        settingsSmbRcvBufSize = prefs.getString(c.getString(R.string.settings_smb_rcv_buf_size), "66576");
        settingsSmbSndBufSize = prefs.getString(c.getString(R.string.settings_smb_snd_buf_size), "66576");
        settingsSmbListSize = prefs.getString(c.getString(R.string.settings_smb_listSize), "");
        settingsSmbMaxBuffers = prefs.getString(c.getString(R.string.settings_smb_maxBuffers), "100");
        settingsSmbTcpNodelay = prefs.getString(c.getString(R.string.settings_smb_tcp_nodelay), "false");

        settingsSmbLmCompatibility = prefs.getString(c.getString(R.string.settings_smb_lm_compatibility), "0");
        boolean ues = prefs.getBoolean(c.getString(R.string.settings_smb_use_extended_security), false);

        settingsSmbUseExtendedSecurity = "";
        if (ues)
            settingsSmbUseExtendedSecurity = "true";
        else
            settingsSmbUseExtendedSecurity = "false";

        jcifs.Config.setProperty("jcifs.netbios.retryTimeout", "3000");

        System.setProperty("jcifs.util.loglevel", settingsSmbLogLevel);
        System.setProperty("jcifs.smb.lmCompatibility", settingsSmbLmCompatibility);
        System.setProperty("jcifs.smb.client.useExtendedSecurity", settingsSmbUseExtendedSecurity);
        System.setProperty("jcifs.smb.client.tcpNoDelay", settingsSmbTcpNodelay);

        if (!settingsSmbRcvBufSize.equals(""))
            System.setProperty("jcifs.smb.client.rcv_buf_size", settingsSmbRcvBufSize);//60416 120832
        if (!settingsSmbSndBufSize.equals(""))
            System.setProperty("jcifs.smb.client.snd_buf_size", settingsSmbSndBufSize);//16644 120832

        if (!settingsSmbListSize.equals(""))
            System.setProperty("jcifs.smb.client.listSize", settingsSmbListSize); //65536 1300
        if (!settingsSmbMaxBuffers.equals(""))
            System.setProperty("jcifs.smb.maxBuffers", settingsSmbMaxBuffers);//16 100

        //      if (mGp.settingDebugLevel>=1) 
        //         mUtil.addDebugMsg(1,"I","JCIFS Option : rcv_buf_size="+settingsSmbRcvBufSize+", "+
        //            "snd_buf_size="+settingsSmbSndBufSize+", "+
        //            "listSize="+settingsSmbListSize+", "+
        //            "maxBuffres="+settingsSmbMaxBuffers+", "+
        //            "tcpNodelay="+settingsSmbTcpNodelay+", "+
        //            "logLevel="+settingsSmbLogLevel+", "+
        //            "lmCompatibility="+settingsSmbLmCompatibility+", "+
        //            "useExtendedSecurity="+settingsSmbUseExtendedSecurity);

    };

}