com.tencent.wstt.gt.activity.GTMainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.tencent.wstt.gt.activity.GTMainActivity.java

Source

/*
 * Tencent is pleased to support the open source community by making
 * Tencent GT (Version 2.4 and subsequent versions) available.
 *
 * Notwithstanding anything to the contrary herein, any previous version
 * of Tencent GT shall not be subject to the license hereunder.
 * All right, title, and interest, including all intellectual property rights,
 * in and to the previous version of Tencent GT (including any and all copies thereof)
 * shall be owned and retained by Tencent and subject to the license under the
 * Tencent GT End User License Agreement (http://gt.qq.com/wp-content/EULA_EN.html).
 * 
 * Copyright (C) 2015 THL A29 Limited, a Tencent company. All rights reserved.
 * 
 * Licensed under the MIT License (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 * 
 * http://opensource.org/licenses/MIT
 * 
 * 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 com.tencent.wstt.gt.activity;

import com.tencent.wstt.gt.GTApp;
import com.tencent.wstt.gt.R;
import com.tencent.wstt.gt.api.utils.Env;
import com.tencent.wstt.gt.dao.GTPref;
import com.tencent.wstt.gt.service.GTFloatView;
import com.tencent.wstt.gt.service.GTLogo;
import com.tencent.wstt.gt.utils.ToastUtil;

import android.Manifest;
import android.app.Notification;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class GTMainActivity extends GTBaseFragmentActivity implements OnClickListener {

    // Android6.x?????MainActivity????
    private static final int REQUEST_NEED_PERMISSION = 101;
    // ???????
    private static final int REQUEST_FLOAT_VIEW = 102;
    private static boolean isFloatViewAllowed = GTPref.getGTPref().getBoolean(GTPref.FLOAT_ALLOWED, false);

    // ?
    private GTAUTFragment autFragment;
    private GTParamTopFragment paramFragment;
    private GTPerfFragment perfFragment;
    private GTLogFragment logFragment;
    private GTPluginFragment pluginFragment;

    // ?
    private View autLayout;
    private View paramLayout;
    private View perfLayout;
    private View logLayout;
    private View pluginLayout;

    // Tab
    private ImageView autImage;
    private ImageView paramImage;
    private ImageView perfImage;
    private ImageView logImage;
    private ImageView pluginImage;

    private TextView autText;
    private TextView paramText;
    private TextView perfText;
    private TextView logText;
    private TextView pluginText;

    public static boolean isActived = false;
    public static Notification notification;
    public static boolean dlgIsShow = false;

    private int curTabId;

    private static GTMainActivity instance;

    public static GTMainActivity getInstance() {
        return instance;
    }

    public GTMainActivity() {
        instance = this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gt_activity_main);
        // ?
        initViews();

        // ???
        removeFragments();

        // ?0tab
        if (savedInstanceState != null) {
            setTabSelection(savedInstanceState.getInt("curTabId"));
        } else {
            setTabSelection(0);
        }
        isActived = true;

        boolean hasPermission = (ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);

        hasPermission = hasPermission && (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);

        hasPermission = hasPermission && (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED);

        hasPermission = hasPermission && (ContextCompat.checkSelfPermission(this,
                Manifest.permission.SYSTEM_ALERT_WINDOW) == PackageManager.PERMISSION_GRANTED);

        if (!hasPermission) {
            ActivityCompat.requestPermissions(this,
                    new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE },
                    REQUEST_NEED_PERMISSION);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
        case REQUEST_NEED_PERMISSION: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // ???do nothing??
                // ??????????ACTION_MANAGE_OVERLAY_PERMISSION??API23?
                if (!isFloatViewAllowed) {
                    requestAlertWindowPermission();
                }
                // ???????
                Env.init();
            } else {
                ToastUtil.ShowLongToast(GTApp.getContext(),
                        "Permission not enough. Please consider granting it this permission.");
            }
        }
        }
    }

    private void requestAlertWindowPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + getPackageName()));
        try {
            startActivityForResult(intent, REQUEST_FLOAT_VIEW);
        } catch (Exception e) {
            // ????
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isActived = false;
    }

    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putInt("curTabId", curTabId);
        super.onSaveInstanceState(savedInstanceState);
    }

    /*
     * Activity???Fragment
     */
    private void removeFragments() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        Fragment fragment = fragmentManager.findFragmentByTag("a");
        if (fragment != null) {
            transaction.remove(fragment);
        }
        fragment = fragmentManager.findFragmentByTag("b");
        if (fragment != null) {
            transaction.remove(fragment);
        }
        fragment = fragmentManager.findFragmentByTag("c");
        if (fragment != null) {
            transaction.remove(fragment);
        }
        fragment = fragmentManager.findFragmentByTag("d");
        if (fragment != null) {
            transaction.remove(fragment);
        }
        fragment = fragmentManager.findFragmentByTag("e");
        if (fragment != null) {
            transaction.remove(fragment);
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * ????
     */
    private void initViews() {
        autLayout = findViewById(R.id.aut_layout);
        paramLayout = findViewById(R.id.param_layout);
        perfLayout = findViewById(R.id.perf_layout);
        logLayout = findViewById(R.id.log_layout);
        pluginLayout = findViewById(R.id.plugin_layout);

        autImage = (ImageView) findViewById(R.id.aut_image);
        paramImage = (ImageView) findViewById(R.id.param_image);
        perfImage = (ImageView) findViewById(R.id.perf_image);
        logImage = (ImageView) findViewById(R.id.log_image);
        pluginImage = (ImageView) findViewById(R.id.plugin_image);

        autText = (TextView) findViewById(R.id.aut_text);
        paramText = (TextView) findViewById(R.id.param_text);
        perfText = (TextView) findViewById(R.id.perf_text);
        logText = (TextView) findViewById(R.id.log_text);
        pluginText = (TextView) findViewById(R.id.plugin_text);

        autLayout.setOnClickListener(this);
        paramLayout.setOnClickListener(this);
        perfLayout.setOnClickListener(this);
        logLayout.setOnClickListener(this);
        pluginLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.aut_layout:
            curTabId = 0;
            setTabSelection(0);
            break;
        case R.id.param_layout:
            curTabId = 1;
            setTabSelection(1);
            break;
        case R.id.perf_layout:
            curTabId = 2;
            setTabSelection(2);
            break;
        case R.id.log_layout:
            curTabId = 3;
            setTabSelection(3);
            break;
        case R.id.plugin_layout:
            curTabId = 4;
            setTabSelection(4);
            break;
        default:
            break;
        }
    }

    /**
     * ?index??tab
     * 
     * @param index
     *            ?tab
     */
    private synchronized void setTabSelection(int index) {
        // ???
        clearSelection();
        // ?Fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // ??FragmentFragment?
        hideFragments(transaction); //TODO

        switch (index) {
        case 0:
            // AUT tab?
            autImage.setImageResource(R.drawable.tab_selected_border);
            autText.setTextColor(Color.WHITE);
            if (autFragment == null) {
                autFragment = new GTAUTFragment();
                transaction.add(R.id.content, autFragment, "a");
            } else {
                transaction.show(autFragment);
            }
            break;
        case 1:
            // ? tab?
            paramImage.setImageResource(R.drawable.tab_selected_border);
            paramText.setTextColor(Color.WHITE);
            if (paramFragment == null) {
                paramFragment = new GTParamTopFragment();
                transaction.add(R.id.content, paramFragment, "b");
            } else {
                transaction.show(paramFragment);
                paramFragment.onShow(true);
            }
            break;
        case 2:
            //  tab?
            perfImage.setImageResource(R.drawable.tab_selected_border);
            perfText.setTextColor(Color.WHITE);
            if (perfFragment == null) {
                perfFragment = new GTPerfFragment();
                transaction.add(R.id.content, perfFragment, "c");
            } else {
                transaction.show(perfFragment);
            }
            break;
        case 3:
            //  tab?
            logImage.setImageResource(R.drawable.tab_selected_border);
            logText.setTextColor(Color.WHITE);
            if (logFragment == null) {
                logFragment = new GTLogFragment();
                transaction.add(R.id.content, logFragment, "d");
            } else {
                transaction.show(logFragment);
            }
            break;
        case 4:
        default:
            // ? tab?
            pluginImage.setImageResource(R.drawable.tab_selected_border);
            pluginText.setTextColor(Color.WHITE);
            if (pluginFragment == null) {
                pluginFragment = new GTPluginFragment();
                transaction.add(R.id.content, pluginFragment, "e");
            } else {
                transaction.show(pluginFragment);
            }
            break;
        }
        /*
         * commit()?
         * IllegalStateException: Can not perform this action after onSaveInstanceState\
         * 
         * @see http://developer.android.com/reference/android/app/FragmentTransaction.html#commitAllowingStateLoss()
         * 
         * ?? commitActivityonSaveInstanceState()?onSaveInstanceState
         * Activity?????Activity?????Fragment
         * commit?? commitAllowingStateLoss()
         */
        // transaction.commit(); 
        transaction.commitAllowingStateLoss();
    }

    /**
     * ?
     */
    private void clearSelection() {
        int defaultColor = getResources().getColor(R.color.tab_default_textcolor);
        autImage.setImageResource(R.drawable.tab_default_border);
        autText.setTextColor(defaultColor);
        paramImage.setImageResource(R.drawable.tab_default_border);
        paramText.setTextColor(defaultColor);
        perfImage.setImageResource(R.drawable.tab_default_border);
        perfText.setTextColor(defaultColor);
        logImage.setImageResource(R.drawable.tab_default_border);
        logText.setTextColor(defaultColor);
        pluginImage.setImageResource(R.drawable.tab_default_border);
        pluginText.setTextColor(defaultColor);
    }

    /**
     * Fragment???
     * 
     * @param transaction
     *            Fragment?
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (autFragment != null) {
            transaction.hide(autFragment);
        }
        if (paramFragment != null) {
            paramFragment.onShow(false);
            transaction.hide(paramFragment);
        }
        if (perfFragment != null) {
            transaction.hide(perfFragment);
        }
        if (logFragment != null) {
            transaction.hide(logFragment);
        }
        if (pluginFragment != null) {
            transaction.hide(pluginFragment);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dlgIsShow = false;

            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int item_id = item.getItemId();
        Intent intent = null;

        switch (item_id) {
        case R.id.exit:
            GTApp.exitGT();
            break;
        case R.id.log_switch:
            intent = new Intent(this, GTLogSwitchActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            break;
        case R.id.air_console:
            intent = new Intent(this, GTACSettingActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            break;
        case R.id.intervals:
            intent = new Intent(this, GTIntervalSettingActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            break;
        case R.id.about:
            intent = new Intent(this, GTAboutActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            break;
        }

        return false;
    }

    /* ????? */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case REQUEST_FLOAT_VIEW:
            // ???????GT
            isFloatViewAllowed = true;
            GTPref.getGTPref().edit().putBoolean(GTPref.FLOAT_ALLOWED, true).commit();

            // ??????
            if (GTPref.getGTPref().getBoolean(GTPref.AC_SWITCH_FLAG, true)) {
                Intent intent = new Intent(this, GTLogo.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startService(intent);

                Intent mintent = new Intent(this, GTFloatView.class);
                mintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startService(mintent);
            }

            break;
        default:
            break;
        }
    }
}