-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from WTFart/develop
Finish CIDR Calculator Application
- Loading branch information
Showing
31 changed files
with
2,653 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
language: android | ||
jdk: oraclejdk8 | ||
sudo: false | ||
env: | ||
global: | ||
- ANDROID_API_LEVEL=26 | ||
- EMULATOR_API_LEVEL=19 | ||
- ANDROID_BUILD_TOOLS_VERSION=26.0.2 | ||
- ADB_INSTALL_TIMEOUT=10 | ||
|
||
android: | ||
components: | ||
- tools | ||
- platform-tools | ||
- tools | ||
- build-tools-$ANDROID_BUILD_TOOLS_VERSION | ||
- android-$EMULATOR_API_LEVEL | ||
- android-$ANDROID_API_LEVEL | ||
- sys-img-armeabi-v7a-android-$EMULATOR_API_LEVEL | ||
- extra-android-m2repository | ||
|
||
before_install: | ||
- mkdir "$ANDROID_HOME/licenses" || true | ||
- echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license" | ||
- echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license" | ||
before_script: | ||
- echo no | android create avd --force -n test -t android-$EMULATOR_API_LEVEL --abi armeabi-v7a | ||
- emulator -avd test -no-skin -no-audio -no-window & | ||
- android-wait-for-emulator | ||
- adb shell input keyevent 82 & | ||
script: | ||
- ./gradlew test | ||
- ./gradlew connectedAndroidTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
app/src/main/java/com/wtfart/ipaddressmanager/CalculatorFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.wtfart.ipaddressmanager | ||
|
||
import android.content.Context | ||
import android.os.Bundle | ||
import android.support.v4.app.Fragment | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ArrayAdapter | ||
import android.widget.Toast | ||
|
||
import kotlinx.android.synthetic.main.fragment_calculator.* | ||
|
||
import com.wtfart.ipaddressmanager.model.Cidr | ||
|
||
class CalculatorFragment : Fragment() { | ||
|
||
companion object { | ||
|
||
@JvmStatic | ||
fun newInstance() = CalculatorFragment() | ||
} | ||
|
||
private lateinit var mListener: MainActivity | ||
|
||
private lateinit var mCidrNotationsAdapter: ArrayAdapter<String> | ||
|
||
private lateinit var mCidrNotations: Array<Cidr> | ||
|
||
override fun onAttach(context: Context?) { | ||
super.onAttach(context) | ||
|
||
mListener = context as MainActivity | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater?, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
return inflater?.inflate(R.layout.fragment_calculator, container, false) | ||
} | ||
|
||
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
mListener.setTitle(R.string.calculator_name) | ||
} | ||
|
||
override fun onActivityCreated(savedInstanceState: Bundle?) { | ||
super.onActivityCreated(savedInstanceState) | ||
|
||
button_calculate.setOnClickListener { | ||
try { | ||
calculate() | ||
mListener.requestFocus() | ||
} catch (e: IllegalArgumentException) { | ||
Toast.makeText(mListener, getString(R.string.calculator_error_input), Toast.LENGTH_LONG).show() | ||
} | ||
} | ||
button_clear.setOnClickListener { | ||
edittext_input_ip_address.setText("") | ||
edittext_input_num_addresses.setText("") | ||
layout_calculator_output.visibility = View.INVISIBLE | ||
} | ||
listview_cidr_notations.setOnItemClickListener { _, _, i, _ -> | ||
mListener.switchFragment(DetailFragment.newInstance(mCidrNotations[i])) | ||
} | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
|
||
try { | ||
calculate() | ||
} catch (e: IllegalArgumentException) { | ||
e.printStackTrace() | ||
} | ||
} | ||
|
||
private fun calculate() { | ||
mCidrNotations = Cidr.compute( | ||
edittext_input_ip_address.text.toString(), | ||
edittext_input_num_addresses.text.toString().toInt() | ||
) | ||
|
||
textview_required_cidr_notations.text = mCidrNotations.size.toString() | ||
mCidrNotationsAdapter = ArrayAdapter( | ||
mListener, | ||
android.R.layout.simple_list_item_1, | ||
mCidrNotations.map { cidrNotation -> cidrNotation.notation } | ||
) | ||
listview_cidr_notations.adapter = mCidrNotationsAdapter | ||
layout_calculator_output.visibility = View.VISIBLE | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
app/src/main/java/com/wtfart/ipaddressmanager/DetailFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.wtfart.ipaddressmanager | ||
|
||
import android.content.Context | ||
import android.os.Bundle | ||
import android.support.v4.app.Fragment | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
|
||
import kotlinx.android.synthetic.main.fragment_detail.* | ||
|
||
import com.wtfart.ipaddressmanager.model.Cidr | ||
import com.wtfart.ipaddressmanager.util.IpConverter | ||
|
||
class DetailFragment : Fragment() { | ||
|
||
companion object { | ||
|
||
private val CIDR_KEY = "CIDR" | ||
|
||
@JvmStatic | ||
fun newInstance(cidr: Cidr): DetailFragment { | ||
val args = Bundle() | ||
args.putSerializable(CIDR_KEY, cidr) | ||
val fragment = DetailFragment() | ||
fragment.arguments = args | ||
|
||
return fragment | ||
} | ||
} | ||
|
||
private lateinit var mListener: MainActivity | ||
|
||
private lateinit var mCidr: Cidr | ||
|
||
override fun onAttach(context: Context?) { | ||
super.onAttach(context) | ||
|
||
mListener = context as MainActivity | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
mCidr = arguments.getSerializable(CIDR_KEY) as Cidr | ||
|
||
mListener.title = mCidr.notation | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater?, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
return inflater?.inflate(R.layout.fragment_detail, container, false) | ||
} | ||
|
||
override fun onActivityCreated(savedInstanceState: Bundle?) { | ||
super.onActivityCreated(savedInstanceState) | ||
|
||
val (notation, netmask, wildcardMask, ipAddressRange) = mCidr | ||
val (initialIpAddress, lastIpAddress) = ipAddressRange | ||
|
||
textview_notation.text = notation | ||
textview_netmask.text = IpConverter.toIpAddress(netmask) | ||
textview_wildcard_mask.text = IpConverter.toIpAddress(wildcardMask) | ||
textview_ip_address_range.text = if (initialIpAddress == lastIpAddress) { | ||
IpConverter.toIpAddress(initialIpAddress) | ||
} else { | ||
getString( | ||
R.string.detail_format_ip_address_range, | ||
IpConverter.toIpAddress(initialIpAddress), | ||
IpConverter.toIpAddress(lastIpAddress) | ||
) | ||
} | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
app/src/main/java/com/wtfart/ipaddressmanager/EmptyActivity.kt
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
app/src/main/java/com/wtfart/ipaddressmanager/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.wtfart.ipaddressmanager | ||
|
||
import android.os.Bundle | ||
import android.support.v4.app.Fragment | ||
import android.support.v7.app.AppCompatActivity | ||
import android.view.MenuItem | ||
|
||
import kotlinx.android.synthetic.main.activity_main.layout_main_activity | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
supportFragmentManager | ||
.beginTransaction() | ||
.add(R.id.fragment_container, CalculatorFragment.newInstance()) | ||
.commit() | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem?): Boolean { | ||
when (item?.itemId) { | ||
android.R.id.home -> { | ||
onBackPressed() | ||
return true | ||
} | ||
} | ||
|
||
return super.onOptionsItemSelected(item) | ||
} | ||
|
||
override fun onBackPressed() { | ||
super.onBackPressed() | ||
|
||
setupHomeButton(false) | ||
} | ||
|
||
fun requestFocus() { | ||
layout_main_activity.requestFocus() | ||
} | ||
|
||
fun switchFragment(fragment: Fragment) { | ||
setupHomeButton(true) | ||
supportFragmentManager | ||
.beginTransaction() | ||
.setCustomAnimations( | ||
R.anim.slide_in_right, | ||
R.anim.slide_out_left, | ||
R.anim.slide_in_left, | ||
R.anim.slide_out_right | ||
) | ||
.replace(R.id.fragment_container, fragment) | ||
.addToBackStack(null) | ||
.commit() | ||
} | ||
|
||
private fun setupHomeButton(status: Boolean) { | ||
if (supportFragmentManager.backStackEntryCount == 0) { | ||
supportActionBar?.setHomeButtonEnabled(status) | ||
supportActionBar?.setDisplayHomeAsUpEnabled(status) | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/wtfart/ipaddressmanager/SplashActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.wtfart.ipaddressmanager | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.os.Handler | ||
import android.support.v7.app.AppCompatActivity | ||
|
||
class SplashActivity : AppCompatActivity() { | ||
|
||
private val mDelayTime = 3000L | ||
|
||
private lateinit var mHandler: Handler | ||
private lateinit var mRunnable: Runnable | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_splash) | ||
|
||
mHandler = Handler() | ||
mRunnable = Runnable { | ||
startActivity(Intent(this@SplashActivity, MainActivity::class.java)) | ||
finish() | ||
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left) | ||
} | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
|
||
mHandler.postDelayed(mRunnable, mDelayTime) | ||
} | ||
|
||
override fun onPause() { | ||
super.onPause() | ||
|
||
mHandler.removeCallbacks(mRunnable) | ||
} | ||
} |
Oops, something went wrong.