Skip to content

Commit

Permalink
There is no need to free the references of context and activity, and …
Browse files Browse the repository at this point in the history
…freeing them would cause null exception when press back button to jump back to previous fragment.

But the app will reports memory leaks when the app is terminated, seems there is still no any good looking solutions to address this issue. See square/leakcanary#1081
  • Loading branch information
simonpunk committed Nov 11, 2019
1 parent 4c6be8e commit 023fe9e
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 535 deletions.
47 changes: 26 additions & 21 deletions src/com/offsec/nethunter/AppNavHomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

Expand Down Expand Up @@ -62,10 +64,8 @@ public class AppNavHomeActivity extends AppCompatActivity implements KaliGPSUpda
private NavigationView navigationView;
private CharSequence mTitle = "NetHunter";
private Stack<String> titles = new Stack<>();
private static SharedPreferences prefs;
private SharedPreferences prefs;
public static MenuItem lastSelectedMenuItem;
public Context context;
public Activity activity;
private boolean locationUpdatesRequested = false;
private KaliGPSUpdates.Receiver locationUpdateReceiver;
private NhPaths nhPaths;
Expand All @@ -78,28 +78,26 @@ public class AppNavHomeActivity extends AppCompatActivity implements KaliGPSUpda
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
this.context = getApplicationContext();
this.activity = this;

// Initiate the NhPaths singleton class, and it will then keep living until the app dies.
// Also with its sharepreference listener registered, the CHROOT_PATH variable can be updated immediately on sharepreference changes.
nhPaths = NhPaths.getInstance(context);
nhPaths = NhPaths.getInstance(getApplicationContext());
// Initiate the PermissionCheck class.
permissionCheck = new PermissionCheck(activity, context);
permissionCheck = new PermissionCheck(this, getApplicationContext());
// Register the nethunter receiver with intent actions.
nethunterReceiver = new NethunterReceiver();
IntentFilter AppNavHomeIntentFilter = new IntentFilter();
AppNavHomeIntentFilter.addAction(NethunterReceiver.CHECKCOMPAT);
AppNavHomeIntentFilter.addAction(NethunterReceiver.BACKPRESSED);
AppNavHomeIntentFilter.addAction(NethunterReceiver.CHECKCHROOT);
AppNavHomeIntentFilter.addAction("ChrootManager");
activity.registerReceiver(nethunterReceiver, new IntentFilter(AppNavHomeIntentFilter));
this.registerReceiver(nethunterReceiver, new IntentFilter(AppNavHomeIntentFilter));
// initiate prefs.
prefs = getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE);

// Start copying the app files to the corresponding path.
ProgressDialog progressDialog = new ProgressDialog(activity);
CopyBootFilesAsyncTask copyBootFilesAsyncTask = new CopyBootFilesAsyncTask(context, activity, progressDialog);
ProgressDialog progressDialog = new ProgressDialog(this);
CopyBootFilesAsyncTask copyBootFilesAsyncTask = new CopyBootFilesAsyncTask(getApplicationContext(), this, progressDialog);
copyBootFilesAsyncTask.setListener(new CopyBootFilesAsyncTask.CopyBootFilesAsyncTaskListener() {
@Override
public void onAsyncTaskPrepare() {
Expand All @@ -126,7 +124,7 @@ public void onAsyncTaskFinished(Object result) {
}

// Thirdly, check if nethunter terminal app has been installed.
if (context.getPackageManager().getLaunchIntentForPackage("com.offsec.nhterm") == null) {
if (getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.offsec.nhterm") == null) {
showWarningDialog("Nethunter app cannot be run properly", "Nethunter terminal is not installed yet.", true);
}

Expand All @@ -141,7 +139,7 @@ public void onAsyncTaskFinished(Object result) {


@Override
public boolean onOptionsItemSelected(MenuItem item) {
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
Expand Down Expand Up @@ -262,7 +260,7 @@ public void onStopRequested() {
@Override
protected void onStart() {
super.onStart();
if (navigationView != null) startService(new Intent(context, CompatCheckService.class));
if (navigationView != null) startService(new Intent(getApplicationContext(), CompatCheckService.class));
}

@Override
Expand Down Expand Up @@ -291,8 +289,9 @@ private void setRootView(){
mDrawerLayout = findViewById(R.id.drawer_layout);

navigationView = findViewById(R.id.navigation_view);
final ViewGroup nullParent = null;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout navigationHeadView = (LinearLayout) inflater.inflate(R.layout.sidenav_header, null);
LinearLayout navigationHeadView = (LinearLayout) inflater.inflate(R.layout.sidenav_header, nullParent);
navigationView.addHeaderView(navigationHeadView);

FloatingActionButton readmeButton = navigationHeadView.findViewById(R.id.info_fab);
Expand All @@ -318,7 +317,7 @@ private void setRootView(){

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// detail for android 5 devices
getWindow().setStatusBarColor(ContextCompat.getColor(activity, R.color.darkTitle));
getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.darkTitle));
}

getSupportFragmentManager()
Expand All @@ -342,10 +341,10 @@ private void setRootView(){
lastSelectedMenuItem = navigationView.getMenu().getItem(0);
lastSelectedMenuItem.setChecked(true);
}
mDrawerToggle = new ActionBarDrawerToggle(activity, mDrawerLayout, R.string.drawer_opened, R.string.drawer_closed);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_opened, R.string.drawer_closed);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
startService(new Intent(context, CompatCheckService.class));
startService(new Intent(getApplicationContext(), CompatCheckService.class));
}

private void checkUpdate() {
Expand All @@ -369,7 +368,9 @@ private void showLicense() {
.setNegativeButton("Close", (dialog, which) -> dialog.cancel()); //nhwarning
AlertDialog ad = adb.create();
ad.setCancelable(false);
ad.getWindow().getAttributes().windowAnimations = R.style.DialogStyle;
if (ad.getWindow() != null) {
ad.getWindow().getAttributes().windowAnimations = R.style.DialogStyle;
}
ad.show();
}

Expand Down Expand Up @@ -438,7 +439,7 @@ private void setupDrawerContent(NavigationView navigationView) {
changeFragment(fragmentManager, MITMfFragment.newInstance(itemId));
break;
case R.id.vnc_item:
if (context.getPackageManager().getLaunchIntentForPackage("com.offsec.nhvnc") == null) {
if (getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.offsec.nhvnc") == null) {
showWarningDialog("", "Nethunter VNC is not installed yet.", false);
} else {
if (!permissionCheck.isAllPermitted(PermissionCheck.NH_VNC_PERMISSIONS)) {
Expand Down Expand Up @@ -521,7 +522,7 @@ private boolean isAllRequiredPermissionsGranted(){
}

public void showWarningDialog(String title, String message, boolean NeedToExit) {
android.app.AlertDialog.Builder warningAD = new android.app.AlertDialog.Builder(activity);
android.app.AlertDialog.Builder warningAD = new android.app.AlertDialog.Builder(this);
warningAD.setCancelable(false);
warningAD.setTitle(title);
warningAD.setMessage(message);
Expand Down Expand Up @@ -569,7 +570,11 @@ public void onReceive(Context context, Intent intent) {
}
}
} catch (Exception e) {
Log.e(AppNavHomeActivity.TAG, e.getMessage());
if (e.getMessage() != null) {
Log.e(AppNavHomeActivity.TAG, e.getMessage());
} else {
Log.e(AppNavHomeActivity.TAG, "e.getMessage is Null.");
}
}
break;
}
Expand Down
5 changes: 1 addition & 4 deletions src/com/offsec/nethunter/ChrootManagerFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
resultViewerLoggerTextView.setMovementMethod(new ScrollingMovementMethod());
kaliFolderTextView.setClickable(true);
kaliFolderTextView.setText(sharedPreferences.getString(SharePrefTag.CHROOT_ARCH_SHAREPREF_TAG, NhPaths.ARCH_FOLDER));
LinearLayoutCompat kaliViewFolderlinearLayout = view.findViewById(R.id.f_chrootmanager_viewholder);
final LinearLayoutCompat kaliViewFolderlinearLayout = view.findViewById(R.id.f_chrootmanager_viewholder);
kaliViewFolderlinearLayout.setOnClickListener(view1 -> new AlertDialog.Builder(activity)
.setMessage(baseChrootPathTextView.getText().toString() +
kaliFolderTextView.getText().toString())
Expand Down Expand Up @@ -144,10 +144,7 @@ public void onDestroyView() {
addMetaPkgButton = null;
removeChrootButton = null;
backupChrootButton = null;
sharedPreferences = null;
chrootManagerAsynctask = null;
context = null;
activity = null;
}

private void setEditButton(){
Expand Down
Loading

0 comments on commit 023fe9e

Please sign in to comment.