com.aqtx.app.main.activity.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.aqtx.app.main.activity.MainActivity.java

Source

package com.aqtx.app.main.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.aqtx.app.BadgeActionProvider;
import com.aqtx.app.RegisterActivity;
import com.aqtx.app.avchat.AVChatProfile;
import com.aqtx.app.avchat.activity.AVChatActivity;
import com.aqtx.app.chatroom.helper.ChatRoomHelper;
import com.aqtx.app.config.preference.Preferences;
import com.aqtx.app.contact.activity.AddFriendActivity;
import com.aqtx.app.contact.activity.AddFriendMenuActivity;
import com.aqtx.app.login.LogoutHelper;
import com.aqtx.app.main.fragment.HomeFragment;
import com.aqtx.app.main.helper.SoftUpdate;
import com.aqtx.app.main.model.Extras;
import com.aqtx.app.session.SessionHelper;
import com.aqtx.app.team.TeamCreateHelper;
import com.aqtx.app.team.activity.AdvancedTeamSearchActivity;
import com.netease.nim.uikit.LoginSyncDataStatusObserver;
import com.netease.nim.uikit.NimUIKit;
import com.netease.nim.uikit.common.activity.UI;
import com.netease.nim.uikit.common.ui.dialog.DialogMaker;
import com.netease.nim.uikit.common.util.log.LogUtil;
import com.netease.nim.uikit.contact_selector.activity.ContactSelectActivity;
import com.netease.nim.uikit.permission.MPermission;
import com.netease.nim.uikit.permission.annotation.OnMPermissionDenied;
import com.netease.nim.uikit.permission.annotation.OnMPermissionGranted;
import com.netease.nim.uikit.team.helper.TeamHelper;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.NimIntent;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.networkbench.agent.impl.NBSAppAgent;

import java.util.ArrayList;

/**
 * ?
 * <p>
 * Created by huangjun on 2015/3/25.
 */
public class MainActivity extends UI {

    private static final String EXTRA_APP_QUIT = "APP_QUIT";
    private static final int REQUEST_CODE_NORMAL = 1;
    private static final int REQUEST_CODE_ADVANCED = 2;
    private static final String TAG = MainActivity.class.getSimpleName();
    private final int BASIC_PERMISSION_REQUEST_CODE = 100;
    //   
    private BadgeActionProvider settingBadgeActionProvider;
    private HomeFragment mainFragment;
    private Menu menu;

    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    // 
    public static void logout(Context context, boolean quit) {
        Intent extra = new Intent();
        extra.putExtra(EXTRA_APP_QUIT, quit);
        start(context, extra);
    }

    @Override
    protected boolean displayHomeAsUpEnabled() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.aqtx.app.R.layout.activity_main_tab);
        setToolBar(com.aqtx.app.R.id.toolbar, com.aqtx.app.R.string.app_name, 0);
        setTitle(com.aqtx.app.R.string.app_name);

        requestBasicPermission();
        onParseIntent();
        // TODO: 2016/11/22 ??
        NBSAppAgent.setLicenseKey("1420fc5d44be40f892bb70b33356dc1f").withLocationServiceEnabled(true)
                .start(this.getApplicationContext());
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            SoftUpdate update = new SoftUpdate(this);
            update.update();
        } else {
            //??,????
            boolean shouldShow = ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (shouldShow) {
                //??
                ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
                        1);
            } else {
                //?
                //TODO ???
                Toast.makeText(this, "??", Toast.LENGTH_SHORT)
                        .show();
            }
        }

        // ???
        boolean syncCompleted = LoginSyncDataStatusObserver.getInstance()
                .observeSyncDataCompletedEvent(new Observer<Void>() {
                    @Override
                    public void onEvent(Void v) {
                        DialogMaker.dismissProgressDialog();
                    }
                });

        Log.i(TAG, "sync completed = " + syncCompleted);
        if (!syncCompleted) {
            DialogMaker.showProgressDialog(MainActivity.this, getString(com.aqtx.app.R.string.prepare_data))
                    .setCanceledOnTouchOutside(false);
        }

        onInit();
    }

    /**
     * ???
     */
    private void requestBasicPermission() {
        MPermission.with(MainActivity.this).addRequestCode(BASIC_PERMISSION_REQUEST_CODE)
                .permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_CONTACTS,
                        Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                .request();
    }

    @OnMPermissionGranted(BASIC_PERMISSION_REQUEST_CODE)
    public void onBasicPermissionSuccess() {
        //        Toast.makeText(this, "??", Toast.LENGTH_SHORT).show();
    }

    @OnMPermissionDenied(BASIC_PERMISSION_REQUEST_CODE)
    public void onBasicPermissionFailed() {
        Toast.makeText(this, "?", Toast.LENGTH_SHORT).show();
    }

    private void onInit() {

        // ?
        showMainFragment();
        // ??
        ChatRoomHelper.init();

        LogUtil.ui("NIM SDK cache path=" + NIMClient.getSdkStorageDirPath());
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
            @NonNull int[] grantResults) {
        MPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //permission granted
                SoftUpdate update = new SoftUpdate(this);
                update.update();
                //TODO ?SD??
            } else {
                //permission denied
                //TODO ???
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        onParseIntent();
    }

    @Override
    public void onBackPressed() {
        if (mainFragment != null) {
            if (mainFragment.onBackPressed()) {
                return;
            } else {
                moveTaskToBack(true);
            }
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.clear();
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(com.aqtx.app.R.menu.main_activity_menu, menu);
        this.menu = menu;
        return true;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        //        settingBadgeActionProvider.setIcon(R.drawable.icon);
    }

    private Intent getDefaultIntent() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        return intent;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        //            case com.aqtx.app.R.id.about:
        //                startActivity(new Intent(MainActivity.this, SettingsActivity.class));
        //                break;
        // TODO: 2017/3/16   ??
        //            case com.aqtx.app.R.id.create_normal_team:
        //                ContactSelectActivity.Option option = TeamHelper.getCreateContactSelectOption(null, 50);
        //                NimUIKit.startContactSelect(MainActivity.this, option, REQUEST_CODE_NORMAL);
        //                break;
        case com.aqtx.app.R.id.create_regular_team:
            ContactSelectActivity.Option advancedOption = TeamHelper.getCreateContactSelectOption(null, 50);
            NimUIKit.startContactSelect(MainActivity.this, advancedOption, REQUEST_CODE_ADVANCED);
            break;
        case com.aqtx.app.R.id.search_advanced_team:
            AdvancedTeamSearchActivity.start(MainActivity.this);
            break;
        case com.aqtx.app.R.id.add_buddy:
            AddFriendMenuActivity.start(MainActivity.this);
            break;
        case com.aqtx.app.R.id.search_btn:
            GlobalSearchActivity.start(MainActivity.this);
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void onParseIntent() {
        Intent intent = getIntent();
        if (intent.hasExtra(NimIntent.EXTRA_NOTIFY_CONTENT)) {
            IMMessage message = (IMMessage) getIntent().getSerializableExtra(NimIntent.EXTRA_NOTIFY_CONTENT);
            switch (message.getSessionType()) {
            case P2P:
                SessionHelper.startP2PSession(this, message.getSessionId());
                break;
            case Team:
                SessionHelper.startTeamSession(this, message.getSessionId());
                break;
            default:
                break;
            }
        } else if (intent.hasExtra(EXTRA_APP_QUIT)) {
            onLogout();
            return;
        } else if (intent.hasExtra(AVChatActivity.INTENT_ACTION_AVCHAT)) {
            if (AVChatProfile.getInstance().isAVChatting()) {
                Intent localIntent = new Intent();
                localIntent.setClass(this, AVChatActivity.class);
                startActivity(localIntent);
            }
        } else if (intent.hasExtra(Extras.EXTRA_JUMP_P2P)) {
            Intent data = intent.getParcelableExtra(Extras.EXTRA_DATA);
            String account = data.getStringExtra(Extras.EXTRA_ACCOUNT);
            if (!TextUtils.isEmpty(account)) {
                SessionHelper.startP2PSession(this, account);
            }
        }
    }

    private void showMainFragment() {
        if (mainFragment == null && !isDestroyedCompatible()) {
            mainFragment = new HomeFragment();
            switchFragmentContent(mainFragment);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_CODE_NORMAL) {
                final ArrayList<String> selected = data.getStringArrayListExtra(ContactSelectActivity.RESULT_DATA);
                if (selected != null && !selected.isEmpty()) {
                    TeamCreateHelper.createNormalTeam(MainActivity.this, selected, false, null);
                } else {
                    Toast.makeText(MainActivity.this, "??", Toast.LENGTH_SHORT)
                            .show();
                }
            } else if (requestCode == REQUEST_CODE_ADVANCED) {
                final ArrayList<String> selected = data.getStringArrayListExtra(ContactSelectActivity.RESULT_DATA);
                TeamCreateHelper.createAdvancedTeam(MainActivity.this, selected);
            }
        }

    }

    // 
    private void onLogout() {
        // ?&?
        LogoutHelper.logout();
        // ?
        RegisterActivity.start(this);
        finish();
    }
}