Skip to content

Commit

Permalink
Enforce context through builder constructor
Browse files Browse the repository at this point in the history
close #25
  • Loading branch information
isuPatches committed Oct 24, 2016
1 parent b0a73d8 commit 2a0f5f8
Show file tree
Hide file tree
Showing 24 changed files with 436 additions and 76 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ WiseFy now supports the builder pattern so cool functionality can be added later
To grab a default instance:

```java
WiseFy mWiseFy = new WiseFy.generator().withContext(getActivity()).getSmarts();
WiseFy mWiseFy = new WiseFy.withContext(getActivity()).getSmarts();
```

To grab an instance with logging enabled:

```java
WiseFy mWiseFy = new WiseFy.generator().withContext(getActivity()).logging(true).getSmarts();
WiseFy mWiseFy = new WiseFy.withContext(getActivity()).logging(true).getSmarts();
```

**IMPORTANT!** Please make sure you call withContext for WiseFy to work properly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void setUp() {

mActivityTestRule.launchActivity(new Intent());

mWiseFy = new WiseFy.generator().withContext(mActivityTestRule.getActivity()).getSmarts();
mWiseFy = new WiseFy.withContext(mActivityTestRule.getActivity()).getSmarts();
}

@Test
Expand Down Expand Up @@ -146,13 +146,13 @@ public void testAddWPA2NetworkNullWifiManager() {

@Test
public void testBuilderWithLoggingFalse() {
WiseFy wiseFy = new WiseFy.generator().logging(false).getSmarts();
WiseFy wiseFy = new WiseFy.withContext(mActivityTestRule.getActivity()).logging(false).getSmarts();
assertEquals(false, wiseFy.isLoggingEnabled());
}

@Test
public void testBuilderWithLoggingTrue() {
WiseFy wiseFy = new WiseFy.generator().logging(true).getSmarts();
WiseFy wiseFy = new WiseFy.withContext(mActivityTestRule.getActivity()).logging(true).getSmarts();
assertEquals(true, wiseFy.isLoggingEnabled());
}

Expand Down
87 changes: 58 additions & 29 deletions app/src/main/java/com/isupatches/wisefy/WiseFy.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/**
* Copyright 2016 by Patches Klinefelter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.isupatches.wisefy;


Expand All @@ -16,6 +32,11 @@
import java.util.List;


/**
* Main class to manipulate and query network settings on an Android device
*
* Uses the builder pattern for creation - {@link withContext}
*/
public class WiseFy {

private static final String TAG = WiseFy.class.getSimpleName();
Expand All @@ -37,64 +58,72 @@ public class WiseFy {
/**
* Private constructor that accepts builder input
*/
private WiseFy(generator generator) {
this.mLoggingEnabled = generator.loggingEnabled;
this.mConnectivityManager = GetManagerUtil.getInstance().getConnectivityManager(generator.context);
this.mWifiManager = GetManagerUtil.getInstance().getWiFiManager(generator.context);
private WiseFy(withContext withContext) {
this.mLoggingEnabled = withContext.loggingEnabled;
this.mConnectivityManager = GetManagerUtil.getInstance().getConnectivityManager(withContext.context);
this.mWifiManager = GetManagerUtil.getInstance().getWiFiManager(withContext.context);
}

/**
* Internal static class for builder pattern
* Static class for builder pattern
*
* Implements builder interfaces #{@link Logging} #{@link GetSmarts}
*/
public static class generator implements GetSmarts, WithContext {
public static class withContext implements Logging, GetSmarts {

private Context context;

private boolean loggingEnabled;

/**
* Mandatory - The context to get a WiFi and Connectivity manager
* Mandatory - The public constructor for the builder that requires a context
*
* @param context - The activity or application context
* {@link WithContext}
* @param context - The activity or application context to get a WifiConfiguration and
* ConnectivityManager instance
*
* @return generator - The builder with updated context
* {@link #WiseFy(withContext)}
*/
public GetSmarts withContext(Context context) {
public withContext(Context context) {
this.context = context;
return this;
}

/**
* Non-mandatory - To enable/disable logging for the WiseFy instance
*
* @param loggingEnabled - If logging is enabled or disabled
* {@link GetSmarts}
* Mandatory - To build and return a WiseFy instance
*
* @return generator - The builder with updated logging setting
* @return WiseFy - The instance created by the builder
*/
public generator logging(boolean loggingEnabled) {
this.loggingEnabled = loggingEnabled;
return this;
@Override
public WiseFy getSmarts() {
return new WiseFy(this);
}

/**
* Mandatory - To build and return a WiseFy instance
* Optional - Builder method that enables/disables logging for a WiseWy instance
*
* @return WiseFy - The instance created by the builder
* @param loggingEnabled - If logging is enabled or disabled for an instance
* {@link Logging}
*
* @return withContext - The builder with updated logging setting
*/
public WiseFy getSmarts() {
return new WiseFy(this);
@Override
public withContext logging(boolean loggingEnabled) {
this.loggingEnabled = loggingEnabled;
return this;
}
}

interface GetSmarts {
WiseFy getSmarts();
/**
* An interface that enables/disables logging for a WiseFy instance
*/
interface Logging {
withContext logging(boolean loggingEnabled);
}

interface WithContext {
GetSmarts withContext(Context context);
generator logging(boolean loggingEnabled);
/**
* An interface that builds a WiseFy instance
*/
interface GetSmarts {
WiseFy getSmarts();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions javadoc/allclasses-frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- NewPage -->
<html lang="en">
<head>
<!-- Generated by javadoc (1.8.0_102) on Sun Oct 23 12:14:54 CDT 2016 -->
<!-- Generated by javadoc (1.8.0_102) on Sun Oct 23 23:59:26 CDT 2016 -->
<title>All Classes (app 1.0.4 API)</title>
<meta name="date" content="2016-10-23">
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
Expand All @@ -16,7 +16,7 @@ <h1 class="bar">All&nbsp;Classes</h1>
<li><a href="com/isupatches/wisefy/util/LogUtil.html" title="class in com.isupatches.wisefy.util" target="classFrame">LogUtil</a></li>
<li><a href="com/isupatches/wisefy/util/SSIDUtil.html" title="class in com.isupatches.wisefy.util" target="classFrame">SSIDUtil</a></li>
<li><a href="com/isupatches/wisefy/WiseFy.html" title="class in com.isupatches.wisefy" target="classFrame">WiseFy</a></li>
<li><a href="com/isupatches/wisefy/WiseFy.generator.html" title="class in com.isupatches.wisefy" target="classFrame">WiseFy.generator</a></li>
<li><a href="com/isupatches/wisefy/WiseFy.withContext.html" title="class in com.isupatches.wisefy" target="classFrame">WiseFy.withContext</a></li>
</ul>
</div>
</body>
Expand Down
4 changes: 2 additions & 2 deletions javadoc/allclasses-noframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- NewPage -->
<html lang="en">
<head>
<!-- Generated by javadoc (1.8.0_102) on Sun Oct 23 12:14:54 CDT 2016 -->
<!-- Generated by javadoc (1.8.0_102) on Sun Oct 23 23:59:26 CDT 2016 -->
<title>All Classes (app 1.0.4 API)</title>
<meta name="date" content="2016-10-23">
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">
Expand All @@ -16,7 +16,7 @@ <h1 class="bar">All&nbsp;Classes</h1>
<li><a href="com/isupatches/wisefy/util/LogUtil.html" title="class in com.isupatches.wisefy.util">LogUtil</a></li>
<li><a href="com/isupatches/wisefy/util/SSIDUtil.html" title="class in com.isupatches.wisefy.util">SSIDUtil</a></li>
<li><a href="com/isupatches/wisefy/WiseFy.html" title="class in com.isupatches.wisefy">WiseFy</a></li>
<li><a href="com/isupatches/wisefy/WiseFy.generator.html" title="class in com.isupatches.wisefy">WiseFy.generator</a></li>
<li><a href="com/isupatches/wisefy/WiseFy.withContext.html" title="class in com.isupatches.wisefy">WiseFy.withContext</a></li>
</ul>
</div>
</body>
Expand Down
15 changes: 10 additions & 5 deletions javadoc/com/isupatches/wisefy/WiseFy.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- NewPage -->
<html lang="en">
<head>
<!-- Generated by javadoc (1.8.0_102) on Sun Oct 23 12:14:54 CDT 2016 -->
<!-- Generated by javadoc (1.8.0_102) on Sun Oct 23 23:59:26 CDT 2016 -->
<title>WiseFy (app 1.0.4 API)</title>
<meta name="date" content="2016-10-23">
<link rel="stylesheet" type="text/css" href="../../../stylesheet.css" title="Style">
Expand Down Expand Up @@ -49,7 +49,7 @@
<div class="subNav">
<ul class="navList">
<li>Prev&nbsp;Class</li>
<li><a href="../../../com/isupatches/wisefy/WiseFy.generator.html" title="class in com.isupatches.wisefy"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
<li><a href="../../../com/isupatches/wisefy/WiseFy.withContext.html" title="class in com.isupatches.wisefy"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
</ul>
<ul class="navList">
<li><a href="../../../index.html?com/isupatches/wisefy/WiseFy.html" target="_top">Frames</a></li>
Expand Down Expand Up @@ -110,6 +110,9 @@ <h2 title="Class WiseFy" class="title">Class WiseFy</h2>
<br>
<pre>public class <span class="typeNameLabel">WiseFy</span>
extends java.lang.Object</pre>
<div class="block">Main class to manipulate and query network settings on an Android device

Uses the builder pattern for creation - <a href="../../../com/isupatches/wisefy/WiseFy.withContext.html" title="class in com.isupatches.wisefy"><code>WiseFy.withContext</code></a></div>
</li>
</ul>
</div>
Expand All @@ -130,8 +133,10 @@ <h3>Nested Class Summary</h3>
</tr>
<tr class="altColor">
<td class="colFirst"><code>static class&nbsp;</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../com/isupatches/wisefy/WiseFy.generator.html" title="class in com.isupatches.wisefy">WiseFy.generator</a></span></code>
<div class="block">Internal static class for builder pattern</div>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../com/isupatches/wisefy/WiseFy.withContext.html" title="class in com.isupatches.wisefy">WiseFy.withContext</a></span></code>
<div class="block">Static class for builder pattern

Implements builder interfaces #<code>Logging</code> #<code>GetSmarts</code></div>
</td>
</tr>
</table>
Expand Down Expand Up @@ -777,7 +782,7 @@ <h4>isLoggingEnabled</h4>
<div class="subNav">
<ul class="navList">
<li>Prev&nbsp;Class</li>
<li><a href="../../../com/isupatches/wisefy/WiseFy.generator.html" title="class in com.isupatches.wisefy"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
<li><a href="../../../com/isupatches/wisefy/WiseFy.withContext.html" title="class in com.isupatches.wisefy"><span class="typeNameLink">Next&nbsp;Class</span></a></li>
</ul>
<ul class="navList">
<li><a href="../../../index.html?com/isupatches/wisefy/WiseFy.html" target="_top">Frames</a></li>
Expand Down
Loading

0 comments on commit 2a0f5f8

Please sign in to comment.