com.hanyao.passwordmanager.activity.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.hanyao.passwordmanager.activity.MainActivity.java

Source

package com.hanyao.passwordmanager.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hanyao.passwordmanager.Adapter.PasswordListAdapter;
import com.hanyao.passwordmanager.Presenter.PasswordPresenter;
import com.hanyao.passwordmanager.R;
import com.hanyao.passwordmanager.bean.Password;
import com.hanyao.passwordmanager.bean.PasswordList;
import com.hanyao.passwordmanager.util.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class MainActivity extends BaseActivity {
    public static MainActivity instance = null;
    private Toolbar toolbar;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;

    private void findViews() {//
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.dl_left);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        instance = this;
        findViews();
        toolbar.setLogo(R.mipmap.ic_launcher);
        toolbar.setTitle(this.getString(R.string.app_name));
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

            }
        };
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        SharedPreferences preferences = getSharedPreferences("data", MODE_PRIVATE);
        preferences.getString("userName", "null");
        if (preferences.getString("userName", "null").equals("null")) {
            Intent intent = new Intent(MainActivity.this, LocalUserRegistActivity.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent(MainActivity.this, CheckPowerActivity.class);
            startActivity(intent);
        }

        //??
        TextView change = (TextView) findViewById(R.id.change_password_item);
        TextView help = (TextView) findViewById(R.id.help_item);
        TextView about = (TextView) findViewById(R.id.about_item);
        TextView input = (TextView) findViewById(R.id.input_item);
        TextView output = (TextView) findViewById(R.id.output_item);
        TextView agreement = (TextView) findViewById(R.id.agreement_item);
        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ChangePasswordActivity.class);
                startActivity(intent);
            }
        });
        agreement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowInformationActivity.actionShowInformationActivity(MainActivity.this, "<h1>??</h1>\n"
                        + "<p>????????????</p>\n"
                        + "<p>??</p>\n"
                        + "<p>????</p>\n"
                        + "<p>???????</p>\n"
                        + "<p>????</p>\n"
                        + "<p>????</p>\n"
                        + "<p>?</p>\n"
                        + "<p>??</p>\n"
                        + "<p>???????<p/>\n"
                        + "<p>????</p>");
            }
        });
        help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowInformationActivity.actionShowInformationActivity(MainActivity.this,
                        "<h1>???</h1><p>???????</p><h1>????</h1><p>?????????????????????</p>");

            }
        });
        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowInformationActivity.actionShowInformationActivity(MainActivity.this, "<h1></h1>\n"
                        + "<p>1.1.0</p>\n"
                        + "<p>??https://github.com/yilihjy/PasswordManager??</p>\n"
                        + "<p>Copyright yilihjy</p>\n"
                        + "<p>Licensed under the Apache License, Version 2.0 (the \"License\");you may not use this file except in compliance with the License.You may obtain a copy of the License at</p>\n"
                        + "<p>http://www.apache.org/licenses/LICENSE-2.0</p>\n"
                        + "<p>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.</p>\n"
                        + "<h2>??</h2>\n"
                        + "<p>GridPasswordView</p>\n" + "<p>Jungerr</p>\n" + "<p>??</p>\n"
                        + "<p>Copyright 2015 jungly</p>\n"
                        + "<p>Licensed under the Apache License, Version 2.0 (the \"License\");you may not use this file except in compliance with the License.You may obtain a copy of the License at</p>\n"
                        + "<p>http://www.apache.org/licenses/LICENSE-2.0</p>\n"
                        + "<p>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.</p>\n"
                        + "<p>?https://github.com/Jungerr/GridPasswordView</p>");

            }
        });
        output.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("??");
                dialog.setMessage("???");
                dialog.setCancelable(false);
                dialog.setPositiveButton("", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        new OutputTask().execute();
                    }
                });
                dialog.setNegativeButton("?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });
        input.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, InputActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        LinearLayout textView = (LinearLayout) findViewById(R.id.no_password_tip);
        textView.setVisibility(View.GONE);
        ListView listView = (ListView) findViewById(R.id.main_list_view);
        final PasswordPresenter passwordPresenter = new PasswordPresenter();
        Map<String[], Password> map = passwordPresenter.getPassword();
        if (map == null) {
            textView.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
            return;
        }
        listView.setVisibility(View.VISIBLE);
        final List<PasswordList> passwordListList = new ArrayList<>();
        for (Map.Entry<String[], Password> entry : map.entrySet()) {
            PasswordList passwordList = new PasswordList(entry.getKey()[0], entry.getKey()[1]);
            passwordList.setPasswordString(entry.getKey()[2]);
            passwordList.setPassword(entry.getValue());
            passwordListList.add(passwordList);
        }
        Collections.sort(passwordListList);
        PasswordListAdapter passwordListAdapter = new PasswordListAdapter(MainActivity.this,
                R.layout.new_password_list_layout, passwordListList);
        listView.setAdapter(passwordListAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PasswordList passwordList = passwordListList.get(position);
                Intent intent = new Intent(MainActivity.this, DisplayPasswordActivity.class);
                intent.putExtra("password", passwordList.getPassword());
                passwordPresenter.addSee(passwordList.getPassword());
                startActivity(intent);
            }
        });
    }

    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
            case R.id.action_add: {
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("?");
                dialog.setItems(new String[] { "???", "????" },
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which) {
                                case 0: {
                                    startActivity(new Intent(MainActivity.this, AddPasswordActivity.class));
                                }
                                    break;
                                case 1: {
                                    startActivity(new Intent(MainActivity.this, AutoPasswordActivity.class));
                                }
                                    break;
                                }
                            }
                        });
                dialog.show();
            }
                break;
            case R.id.action_exit:
                ActivityCollector.finishAll();
                break;
            }
            return true;
        }
    };

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

    class OutputTask extends AsyncTask<Void, Intent, Boolean> {

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                return new PasswordPresenter().outputPasswords();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (result) {
                if (android.os.Build.VERSION.SDK_INT == 19) {
                    Toast.makeText(MainActivity.this,
                            "???\n?"
                                    + getApplicationContext().getFilesDir().getAbsolutePath() + "/passwords.xml",
                            Toast.LENGTH_LONG).show();
                } else {
                    String path = new File(Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath())
                                    .getParent();
                    Toast.makeText(MainActivity.this,
                            "???\n?" + path + "/passwords.xml",
                            Toast.LENGTH_LONG).show();
                }

            } else {
                Toast.makeText(MainActivity.this, "??", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(findViewById(R.id.left_drawer))) {
            mDrawerLayout.closeDrawers();
        } else
            super.onBackPressed();
    }
}