Skip to content

Commit

Permalink
Added lifecycle trackers to AppMem.java. Seems to function correctly …
Browse files Browse the repository at this point in the history
…on API 30.
  • Loading branch information
salehjg committed Jan 11, 2022
1 parent 6888ed5 commit 1b41750
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 24 deletions.
63 changes: 58 additions & 5 deletions app/src/main/java/io/github/salehjg/pocketzotero/AppMem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.ProgressBar;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;

import com.lmntrx.android.library.livin.missme.ProgressDialog;

import java.util.Vector;

import io.github.salehjg.pocketzotero.zoteroengine.ZoteroEngine;
import io.github.salehjg.pocketzotero.zoteroengine.types.Collection;
import io.github.salehjg.pocketzotero.zoteroengine.types.ItemDetailed;

// This class is used to hold global stuff in memory.
public class AppMem extends Application {
public class AppMem extends Application implements Application.ActivityLifecycleCallbacks {

private Vector<RecordedStatus> mRecordedStatuses;

Expand All @@ -30,12 +31,20 @@ public class AppMem extends Application {
private boolean mProgressDialogCreated;
private ProgressDialogListener mProgressDialogListener;

private int mActivityReferences;
private boolean mIsActivityChangingConfigurations;
private boolean mIsThisAppsStartup;

@Override
public void onCreate() {
super.onCreate();

mPreparation = new Preparation();
mRecordedStatuses = new Vector<>();
super.onCreate();
mActivityReferences = 0;
mIsActivityChangingConfigurations = false;
mIsThisAppsStartup = false;
registerActivityLifecycleCallbacks(this);
}

public interface ProgressDialogListener{
Expand Down Expand Up @@ -259,4 +268,48 @@ public String getStorageSmbServerPassword(){
return reader.getString(getString(R.string.prefKey_storageSmbServerPassword_string), "");
}

// ========================= App Life Cycle Trackers Below =====================================

@Override
public void onActivityStarted(Activity activity) {
if (++mActivityReferences == 1 && !mIsActivityChangingConfigurations) {
if(!mIsThisAppsStartup){
// App enters foreground and its not the app's launch.
Toast.makeText(getApplicationContext(), "Processing the pending attachments", Toast.LENGTH_SHORT).show();
mPreparation.processPendingAttachments(1000);
}else{
mIsThisAppsStartup = false; // bypass if this callback is fired cuz of the app's startup.
}
}
}

@Override
public void onActivityStopped(@NonNull Activity activity) {
mIsActivityChangingConfigurations = activity.isChangingConfigurations();
if (--mActivityReferences == 0 && !mIsActivityChangingConfigurations) {
// App enters background
//Toast.makeText(getApplicationContext(), "Is in background.", Toast.LENGTH_LONG).show();
}
}

@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
if(!mIsActivityChangingConfigurations) {
//Toast.makeText(getApplicationContext(), "Activity created.", Toast.LENGTH_LONG).show();
mIsThisAppsStartup = true;
}
}

@Override
public void onActivityResumed(@NonNull Activity activity) { }

@Override
public void onActivityPaused(@NonNull Activity activity) { }

@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) { }

@Override
public void onActivityDestroyed(@NonNull Activity activity) { }

}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ public void onFinished() {
mAppMem.closeProgressDialog();

// Since we are managed to connect to the host, lets process all the pending attachments
processPendingAttachments();
// also, we dont need startup delay here, its the app's startup.
// the possibility for a third party app writing the file to pocket zotero is zero.
processPendingAttachments(0);
}else{
// ERROR FAILED TO RENAME `DbFileNameSmbTemp` TO `DbFileNameSmb`
mAppMem.recordStatusSingle(mStartupSequenceStatus = STATUS_BASE_STORAGE + 6);
Expand Down Expand Up @@ -214,7 +216,7 @@ public void onCollectionSelected(Collection selectedCollectionObject, Vector<Col
mZoteroEngine.getGuiCollections(linearLayoutCollections);
}

public void processPendingAttachments(){
public void processPendingAttachments(int delayMilliseconds){
File[] pendingsDir = AppDirs.getPredefinedPrivateStoragePending(mContext).listFiles();
Gson gson = new Gson();
if(pendingsDir!=null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,28 +306,13 @@ public void onStartupSequenceCompleted(int statusCode) {
}

@Override
protected void onPause() {
super.onPause();
//deleteFragment(mFragmentMain);

protected void onResume() {
super.onResume();
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
/*
FragmentManager fm = getSupportFragmentManager();
boolean isAddedMain = mFragmentMain.isAdded();
boolean isAddedSettings = mFragmentSettings.isAdded();
boolean isAddedAbout = mFragmentAbout.isAdded();
boolean isAddedStatus = mFragmentStatus.isAdded();
if(isAddedMain) fm.putFragment(outState, STATE_FRAGMENT_MAIN, mFragmentMain);
if(isAddedSettings) fm.putFragment(outState, STATE_FRAGMENT_SETTINGS, mFragmentSettings);
if(isAddedAbout) fm.putFragment(outState, STATE_FRAGMENT_ABOUT, mFragmentAbout);
if(isAddedStatus) fm.putFragment(outState, STATE_FRAGMENT_STATUS, mFragmentStatus);
*/

outState.putSerializable(STATE_DATA_SELECTED_COLLECTION, mDataSelectedCollection);
outState.putSerializable(STATE_DATA_SELECTED_ITEM, mDataSelectedItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,20 @@ void onFinished(
private Vector<Runnable> mRunnables;
private Listener mListener;

private int mStartupDelayMs;

public SmbSendMultipleToHost(
SmbServerInfo serverInfo,
Vector<String> filePathsToSend,
Vector<String> filePathsOnHost,
Vector<Boolean> overWrites,
int concurrentTransferCount,
int startupDelayMs,
Listener listener){
assert filePathsToSend.size() == filePathsOnHost.size();
assert filePathsOnHost.size() == overWrites.size();

mStartupDelayMs = startupDelayMs;
mFilePathsToSend = filePathsToSend;
mFilePathsOnHost = filePathsOnHost;
mOverWrites = overWrites;
Expand Down Expand Up @@ -124,6 +129,10 @@ public void run() {

private void doFileTransferAt(int index){
try {
if(index<mConcurrentTransferCount){
Thread.sleep(mStartupDelayMs);
}

progressUpdateAt(index, 0);
Log.w("SmbSendFileToHost2", "Code0");
SMBClient client = new SMBClient();
Expand Down Expand Up @@ -219,6 +228,26 @@ public SmbSendMultipleToHost(
filePathsOnHost,
getOverWriteVector(overWriteAll, filePathsToSend.size()),
concurrentTransferCount,
0,
listener
);
}

public SmbSendMultipleToHost(
SmbServerInfo serverInfo,
Vector<String> filePathsToSend,
Vector<String> filePathsOnHost,
boolean overWriteAll,
int concurrentTransferCount,
int startDelayMs,
Listener listener){
this(
serverInfo,
filePathsToSend,
filePathsOnHost,
getOverWriteVector(overWriteAll, filePathsToSend.size()),
concurrentTransferCount,
startDelayMs,
listener
);
}
Expand Down

0 comments on commit 1b41750

Please sign in to comment.