Skip to content

Commit

Permalink
begun fixing memleaks
Browse files Browse the repository at this point in the history
  • Loading branch information
rubienr committed Oct 12, 2014
1 parent 4ddf723 commit 3b7510e
Show file tree
Hide file tree
Showing 43 changed files with 852 additions and 412 deletions.
6 changes: 3 additions & 3 deletions source/WebSMSTool/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true" >
android:label="@string/app_name" >
<activity
android:name="at.tugraz.ist.akm.activities.MainActivity"
android:label="@string/app_name"
Expand All @@ -60,7 +59,8 @@

<service
android:name="at.tugraz.ist.akm.webservice.service.WebSMSToolService"
android:exported="false" android:process=":process">
android:exported="false"
android:process=":process" >
</service>

<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public IncomingServiceMessageHandler(StartServiceFragment client)
}


public void onClose()
{
mClientFragment = null;
}


@Override
public void handleMessage(Message msg)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class MainActivity extends Activity
private int mCurrentNavigationDrawerEntry = 0;

private LogClient mLog = new LogClient(this);
private AndroidUILogSink mAndroidUiLogSink = null;
final String mServiceName = WebSMSToolService.class.getName();

private String[] mDrawerEntryTitles = null;
Expand Down Expand Up @@ -112,6 +113,9 @@ protected void onStop()
protected void onDestroy()
{
mLog.debug("activity goes to Hades");
invalidateDrawerList();
mAndroidUiLogSink.onClose();
mAndroidUiLogSink = null;
mLog = null;
super.onDestroy();
}
Expand Down Expand Up @@ -205,11 +209,26 @@ public void onDrawerClosed(View drawerView)
mDrawerList.setItemChecked(mCurrentNavigationDrawerEntry, true);
setUpDrawerToggle();
// TODO: replace sink with a kind of buffered logging sink
TraceService.setSink(new AndroidUILogSink(this));
mAndroidUiLogSink = new AndroidUILogSink(this);
TraceService.setSink(mAndroidUiLogSink);
mLog.debug("launched activity on device [" + Build.PRODUCT + "]");
}


private void invalidateDrawerList()
{
mDrawerEntryTitles = null;
mDrawerFragments = null;
mDrawerLayout.setDrawerListener(null);
mDrawerLayout = null;
mDrawerIcons = null;
mDrawerList.setAdapter(null);
mDrawerList.setOnItemClickListener(null);
mDrawerList = null;
mDrawerToggle = null;
}


@Override
protected void onSaveInstanceState(Bundle outState)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ public class StartServiceFragment extends Fragment implements
LogClient mLog = new LogClient(
StartServiceFragment.class.getCanonicalName());

private Intent mStartSmsServiceIntent = null;
// private Intent mStartSmsServiceIntent = null;
final String mServiceName = WebSMSToolService.class.getName();
private ToggleButton mButton = null;
private TextView mInfoFieldView = null;

private Class<WebSMSToolService> mServiceClass = WebSMSToolService.class;
private final String mServiceComponentNameSuffix = mServiceClass
.getSimpleName();

private WifiIpAddress mWifiState = null;
private ProgressBar mProgressBar = null;

Expand All @@ -59,9 +55,14 @@ public class StartServiceFragment extends Fragment implements
TextView mAccessRestrictionPassword = null;
LinearLayout mAccessRestrictionLayout = null;

private Boolean mIsBoundToService = false;

private Messenger mServiceMessenger = null;

private IncomingServiceMessageHandler mIncomingServiceMessageHandler = new IncomingServiceMessageHandler(
this);
final Messenger mClientMessenger = new Messenger(
new IncomingServiceMessageHandler(this));
mIncomingServiceMessageHandler);

private ServiceRunningStates mServiceRunningState = ServiceRunningStates.BEFORE_SINGULARITY;

Expand All @@ -83,23 +84,44 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
}


@Override
public void onDestroyView()
{
invalidateMainFragmentUI();
super.onDestroyView();
}


@Override
public void onDestroy()
{
stopService();
mLog.debug("on destroy");
mStartSmsServiceIntent = null;
mLog = null;
mButton = null;
mInfoFieldView = null;
unbindFromService();
mIncomingServiceMessageHandler.onClose();
mIncomingServiceMessageHandler = null;
mLog = null;
super.onDestroy();
}


private void unbindFromService()
{
if (mIsBoundToService)
{
getActivity().getApplicationContext().unbindService(this);
mIsBoundToService = false;
}
}


@Override
public void onStart()
{
super.onStart();
setUpMainFragmentUI(getView());
mLog.debug("on start");
}

Expand All @@ -118,8 +140,19 @@ public void onResume()
super.onResume();
mLog.debug("on resume");
setServiceDisabledUI();
getActivity().getApplicationContext().bindService(
mStartSmsServiceIntent, this, Context.BIND_AUTO_CREATE);
bindToService();
}


private void bindToService()
{
if (mIsBoundToService == false)
{
getActivity().getApplicationContext().bindService(
new Intent(getActivity(), WebSMSToolService.class), this,
Context.BIND_AUTO_CREATE);
mIsBoundToService = true;
}
}


Expand All @@ -134,7 +167,6 @@ public void onPause()

private void setUpMainFragmentUI(View view)
{
mStartSmsServiceIntent = new Intent(getActivity(), mServiceClass);
mButton = (ToggleButton) view.findViewById(R.id.start_stop_server);
mInfoFieldView = (TextView) view.findViewById(R.id.adress_data_field);
mButton.setOnClickListener(this);
Expand Down Expand Up @@ -162,6 +194,24 @@ private void setUpMainFragmentUI(View view)
}


private void invalidateMainFragmentUI()
{
mButton.setOnClickListener(null);
mButton = null;
mInfoFieldView = null;
mWifiState.onClose();
mWifiState = null;
mProgressBar = null;
mSmsRecievedView = null;
mSmsSentView = null;
mNetworkRxBytes = null;
mNetworkTxBytes = null;
mAccessRestrictionUsername = null;
mAccessRestrictionPassword = null;
mAccessRestrictionLayout = null;
}


private void tearDownMainFragmentUI()
{
mButton.setOnClickListener(null);
Expand All @@ -180,14 +230,11 @@ public void onClick(View view)
mLog.info("starting web service");
displayStartingService();

// TODO: we are bound here, send server config again before asking for start
// TODO: we are bound here, send server config again before
// asking for start
mProgressBar.setIndeterminate(true);
getActivity().getApplicationContext().startService(
mStartSmsServiceIntent);

// getActivity().getApplicationContext().bindService(
// mStartSmsServiceIntent, StartServiceFragment.this,
// Context.BIND_AUTO_CREATE);
startService();

} else
{
displayNoWifiConnected();
Expand All @@ -201,7 +248,7 @@ public void onClick(View view)
askWebServiceForServiceStopAsync();
try
{
getActivity().getApplicationContext().unbindService(this);
unbindFromService();
}
catch (IllegalArgumentException iae)
{
Expand All @@ -212,6 +259,20 @@ public void onClick(View view)
}


private void startService()
{
getActivity().getApplicationContext().startService(
new Intent(getActivity(), WebSMSToolService.class));
}


private void stopService()
{
getActivity().getApplicationContext().stopService(
new Intent(getActivity(), WebSMSToolService.class));
}


private boolean isServiceRunning()
{
mLog.debug("service is in runningstate ["
Expand Down Expand Up @@ -326,16 +387,18 @@ protected void onWebServiceClientRegistered()
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
String serviceComponentNameSuffix = WebSMSToolService.class
.getSimpleName();
String inServiceName = name.flattenToShortString();
if (inServiceName.endsWith(mServiceComponentNameSuffix))
if (inServiceName.endsWith(serviceComponentNameSuffix))
{
mLog.debug("bound to service [" + mServiceComponentNameSuffix + "]");
mLog.debug("bound to service [" + serviceComponentNameSuffix + "]");
mServiceMessenger = new Messenger(service);
askWebServiceForClientRegistrationAsync();
} else
{
mLog.error("failed binding fragment to service[" + inServiceName
+ "] expected [*" + mServiceComponentNameSuffix + "]");
+ "] expected [*" + serviceComponentNameSuffix + "]");
}
}

Expand All @@ -344,15 +407,17 @@ public void onServiceConnected(ComponentName name, IBinder service)
public void onServiceDisconnected(ComponentName name)
{
String inServiceName = name.flattenToShortString();
if (inServiceName.endsWith(mServiceComponentNameSuffix))
String serviceComponentNameSuffix = WebSMSToolService.class
.getSimpleName();
if (inServiceName.endsWith(serviceComponentNameSuffix))
{
mLog.debug("unbound from service [" + mServiceComponentNameSuffix
mLog.debug("unbound from service [" + serviceComponentNameSuffix
+ "]");
mServiceMessenger = null;
} else
{
mLog.error("failed unbinding fragment to service[" + inServiceName
+ "] expected [*" + mServiceComponentNameSuffix + "]");
+ "] expected [*" + serviceComponentNameSuffix + "]");
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public void onPause()
public void onStop()
{
mLog.debug("fragment onStop()");
mPreferenceChangedListener.onClose();
mPreferenceChangedListener = null;
super.onStop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,11 @@ public void verbose(final String tag, final String message)
}
android.util.Log.v(tag, message);
}

@Override
public void onClose()
{
mActivity = null;
mLogView = null;
}
}
Loading

0 comments on commit 3b7510e

Please sign in to comment.