Added comments and summaries to MainActivity.java and changed variable names

This commit is contained in:
Chris 2019-11-12 18:48:59 -05:00
parent ab6c021c1c
commit e1636754fd
1 changed files with 75 additions and 42 deletions

View File

@ -2,86 +2,119 @@ package com.MartyrPher.smapiandroidinstaller;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
//Tag used for debugging
private static final String TAG = "MainActivity";
private static final int MY_PERMISSION_REQUEST_STORAGE = 2;
//Override int for onRequestionPermssionResult to ensure the right permssion
private static final int PERMISSION_REQUEST_STORAGE = 2;
private boolean hasPermissions = false;
//Whether the user has permissions
public static boolean mHasPermissions = false;
//Whether ConfigEditorFragment has found the mods in the mod folder
public static boolean mHasFoundMods = false;
//TabAdapter to allow changes to the tabs
private TabAdapter mTabAdapter;
//TabLayout to control the tabs at the top of the screen
private TabLayout mTabLayout;
//ViewPager that allows switching between fragments
private ViewPager mViewPager;
/**
* Override that create the initial view and sets up the layout
* @param savedInstanceState = The savedInstanceState
*/
@Override
protected void onCreate(final Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.view_pager);
requestPermissions();
//Find the ViewPager and TabLayout ids
mViewPager = findViewById(R.id.view_pager);
mTabLayout = findViewById(R.id.tab_layout);
final Button start_button = findViewById(R.id.start_button);
start_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start_button.setBackgroundColor(getResources().getColor(R.color.colorAccent));
if (hasPermissions)
//Get the fragment manager and add the fragments
mTabAdapter = new TabAdapter(getSupportFragmentManager());
if (mTabAdapter.getCount() < 1)
{
boolean[] foundGame;
ApkExtractor apkExtractor = new ApkExtractor(MainActivity.this);
foundGame = apkExtractor.checkForInstallOrUpgrade();
if(foundGame[1] || foundGame[2])
{
BackgroundTask backgroundTask = new BackgroundTask(MainActivity.this, apkExtractor, foundGame[0]);
backgroundTask.execute();
}
else
{
DialogFrag.showDialog(MainActivity.this, R.string.cant_find, 1);
start_button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
mTabAdapter.addFragment(new InstallFragment(), "Install");
mTabAdapter.addFragment(new ConfigEditorFragment(), "Configs");
}
}
else
{
//Setup the ViewPager adapter and TabLayout with ViewPager
mViewPager.setAdapter(mTabAdapter);
mTabLayout.setupWithViewPager(mViewPager);
//Request the permissions for storage access
requestPermissions();
}
catch (Exception e)
{
//Try to log a crash in onCreate()
File logFile = new File(Environment.getExternalStorageDirectory() + "/SMAPI Installer/crash.txt");
try {
FileOutputStream stream = new FileOutputStream(logFile);
stream.write(e.getMessage().getBytes());
stream.close();
}
catch(Exception er)
{
Log.e(TAG, er.toString());
}
}
});
}
//Request permissions to be able to read/write external storage
/**
* Request permissions to be able to read/write external storage
*/
public void requestPermissions()
{
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSION_REQUEST_STORAGE);
PERMISSION_REQUEST_STORAGE);
}
else
{
hasPermissions = true;
mHasPermissions = true;
}
}
/**
* Override to handle the result of the permission request
* @param requestCode = The request code
* @param permissions = An array of Strings for needed permissions
* @param grantResults = The results of the permissions
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch(requestCode)
{
case MY_PERMISSION_REQUEST_STORAGE:
case PERMISSION_REQUEST_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
hasPermissions = true;
mHasPermissions = true;
Toast.makeText(this, R.string.permission, Toast.LENGTH_SHORT).show();
}
break;