Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved AndroidPlatformStrategy & Factory, to avoid memory leak of Context #51

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;

import android.app.Activity;
import android.widget.TextView;
import android.util.Log;

/**
Expand All @@ -16,24 +14,14 @@
* "Concrete Strategy" in the Strategy pattern.
*/
public class AndroidPlatformStrategy extends PlatformStrategy
{
/** TextViewVariable. */
private TextView mTextViewOutput;

{
/** Activity variable finds gui widgets by view. */
private WeakReference<Activity> mActivity;
private WeakReference<OutputTextViewActivity> mActivity;

public AndroidPlatformStrategy(Object output,
final Object activityParam)
public AndroidPlatformStrategy(final OutputTextViewActivity activityParam)
{
/**
* A textview output which displays calculations and
* expression trees.
*/
mTextViewOutput = (TextView) output;

/** The current activity window (succinct or verbose). */
mActivity = new WeakReference<Activity>((Activity) activityParam);
/** The current activity window (succinct or verbose). */
mActivity = new WeakReference<OutputTextViewActivity>(activityParam);
}

/**
Expand Down Expand Up @@ -61,7 +49,7 @@ public void print(final String outputString)

/** Indicate that a game thread has finished running. */
public void done()
{
{
// TODO - You fill in here.
}

Expand All @@ -79,4 +67,4 @@ public void errorLog(String javaFile, String errorMessage)
{
Log.e(javaFile, errorMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public static void main(String[] args)
* ConsolePlatform.
*/
PlatformStrategy.instance
(new PlatformStrategyFactory(System.out,
null).makePlatformStrategy());
(new PlatformStrategyFactory(System.out).makePlatformStrategy());

/** Initializes the Options singleton. */
Options.instance().parseArgs(args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package edu.vuum.mocca;

import android.app.Activity;
import android.widget.TextView;

/**
* Abstract class, that defines the the getOutputTextView() method.
*/
public abstract class OutputTextViewActivity extends Activity {

abstract TextView getOutputTextView();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.vuum.mocca;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Expand All @@ -11,7 +10,7 @@
*
* @brief Initial start up screen for the android GUI.
*/
public class PingPongActivity extends Activity {
public class PingPongActivity extends OutputTextViewActivity {
/** TextView that PingPong will be "played" upon */
private TextView mAndroidPingPongOutput;

Expand All @@ -28,19 +27,16 @@ protected void onCreate(Bundle savedInstanceState) {

// Sets the content view to the xml file, activity_ping_pong.
setContentView(R.layout.activity_ping_pong);
mAndroidPingPongOutput =
(TextView) findViewById(R.id.pingpong_output);
mAndroidPingPongOutput = (TextView) findViewById(R.id.pingpong_output);
mPlayButton = (Button) findViewById(R.id.play_button);

// Initializes the Platform singleton with the appropriate
// Platform strategy, which in this case will be the
// AndroidPlatform.
PlatformStrategy.instance
(new PlatformStrategyFactory
(mAndroidPingPongOutput,
this).makePlatformStrategy());
PlatformStrategy.instance(new PlatformStrategyFactory(this)
.makePlatformStrategy());

// Initializes the Options singleton.
// Initializes the Options singleton.
Options.instance().parseArgs(null);
}

Expand All @@ -49,11 +45,10 @@ public void playButtonClicked(View view) {
if (mGameState == PLAY) {
// Use a factory method to create the appropriate type of
// OutputStrategy.
PlayPingPong pingPong =
new PlayPingPong(PlatformStrategy.instance(),
Options.instance().maxIterations(),
Options.instance().maxTurns(),
Options.instance().syncMechanism());
PlayPingPong pingPong = new PlayPingPong(
PlatformStrategy.instance(), Options.instance()
.maxIterations(), Options.instance().maxTurns(),
Options.instance().syncMechanism());

// Play ping-pong with the designated number of
// iterations.
Expand All @@ -73,4 +68,12 @@ public void playButtonClicked(View view) {
mGameState = RESET;
}
}

/**
* grant access of the Output TextView to the AndroidPlatformStrategy
*/
@Override
TextView getOutputTextView() {
return mAndroidPingPongOutput;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,106 +2,98 @@

import java.util.HashMap;

import android.app.Activity;

/**
* @class PlatformStrategyFactory
*
* @brief This class is a factory that is responsible for building the
* designated @a PlatformStrategy implementation at runtime.
*/
public class PlatformStrategyFactory
{
/**
* This interface uses the Strategy pattern to create @a
* PlatformStrategy implementations at runtime.
public class PlatformStrategyFactory {
/**
* This interface uses the Strategy pattern to create @a PlatformStrategy
* implementations at runtime.
*/
private static interface IPlatformStrategyFactoryStrategy
{
private static interface IPlatformStrategyFactoryStrategy {
public PlatformStrategy execute();
}

/**
* Enumeration distinguishing platforms Android from plain ol' Java.
*/
public enum PlatformType {
ANDROID,
PLAIN_JAVA
ANDROID, PLAIN_JAVA
}

/**
* HashMap used to map strings containing the Java platform names
* and dispatch the execute() method of the associated @a PlatformStrategy
* HashMap used to map strings containing the Java platform names and
* dispatch the execute() method of the associated @a PlatformStrategy
* implementation.
*/
private HashMap<PlatformType, IPlatformStrategyFactoryStrategy> mPlatformStrategyMap =
new HashMap<PlatformType, IPlatformStrategyFactoryStrategy>();

/**
* Ctor that stores the objects that perform output for a
* particular platform, such as ConsolePlatformStrategy or the
* AndroidPlatformStrategy.
private HashMap<PlatformType, IPlatformStrategyFactoryStrategy> mPlatformStrategyMap = new HashMap<PlatformType, IPlatformStrategyFactoryStrategy>();

/**
* Ctor that stores the objects that perform output for a particular
* platform, such as ConsolePlatformStrategy or the AndroidPlatformStrategy.
*/
public PlatformStrategyFactory(final Object output,
final Object activity)
{
/**
* The "The Android Project" string maps to a command object
* that creates an @a AndroidPlatformStrategy implementation.
public PlatformStrategyFactory(final Object output) {
/**
* The "The Android Project" string maps to a command object that
* creates an @a AndroidPlatformStrategy implementation.
*/
mPlatformStrategyMap.put(PlatformType.ANDROID,
new IPlatformStrategyFactoryStrategy()
{
/**
* Receives the three parameters, input
* (EditText), output (TextView), activity
* (activity).
*/
public PlatformStrategy execute()
{
return new AndroidPlatformStrategy(output,
activity);
}
});

/**
* The "Sun Microsystems Inc." string maps to a command object
* that creates an @a ConsolePlatformStrategy implementation.
new IPlatformStrategyFactoryStrategy() {
/**
* Receives the three parameters, input (EditText), output
* (TextView), activity (activity).
*
* @throws Exception
*/
public PlatformStrategy execute() {
if (output instanceof OutputTextViewActivity) {
return new AndroidPlatformStrategy(
(OutputTextViewActivity) output);
} else {
return new AndroidPlatformStrategy(null);
}
}
});

/**
* The "Sun Microsystems Inc." string maps to a command object that
* creates an @a ConsolePlatformStrategy implementation.
*/
mPlatformStrategyMap.put(PlatformType.PLAIN_JAVA,
new IPlatformStrategyFactoryStrategy()
{
public PlatformStrategy execute()
{
return new ConsolePlatformStrategy(output);
}
});
new IPlatformStrategyFactoryStrategy() {
public PlatformStrategy execute() {
return new ConsolePlatformStrategy(output);
}
});
}

/**
* Returns the name of the platform in a string. e.g., Android or
* a JVM.
/**
* Returns the name of the platform in a string. e.g., Android or a JVM.
*/
public static String platformName()
{
public static String platformName() {
return System.getProperty("java.specification.vendor");
}

/**
* Returns the type of the platformm e.g. Android or
* a JVM.

/**
* Returns the type of the platformm e.g. Android or a JVM.
*/
public static PlatformType platformType() {
if(platformName().indexOf("Android") >= 0)
if (platformName().indexOf("Android") >= 0)
return PlatformType.ANDROID;
else
else
return PlatformType.PLAIN_JAVA;
}

/**
/**
* Create a new @a PlatformStrategy object based on underlying Java
* platform.
*/
public PlatformStrategy makePlatformStrategy()
{
public PlatformStrategy makePlatformStrategy() {
PlatformType type = platformType();

return mPlatformStrategyMap.get(type).execute();
Expand Down
15 changes: 8 additions & 7 deletions ex/AcronymApplication/res/layout/acronym_data_row.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@
tools:context="edu.vu.isis.serviceparcelableexasync2.MainActivity$PlaceholderFragment" >

<TextView
android:id="@+id/name"
android:id="@+id/year_added_to_db"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/result_string" />
android:text="@string/year_string" />

<TextView
android:id="@+id/db_refs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/year_added_to_db"
android:paddingLeft="5dp"
android:layout_toRightOf="@+id/year_added_to_db"
android:text="@string/refernces_string" />

<TextView
android:id="@+id/year_added_to_db"
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingLeft="50dp"
android:text="@string/year_string" />
android:paddingLeft="5dp"
android:layout_toRightOf="@+id/db_refs"
android:text="@string/result_string" />

</RelativeLayout>
2 changes: 1 addition & 1 deletion ex/AcronymApplication/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<string name="action_settings">Settings</string>
<string name="row_result_tv">Result</string>
<string name="result_string">Result</string>
<string name="refernces_string">References</string>
<string name="refernces_string">Refs</string>
<string name="year_string">Year</string>

</resources>
Loading