Skip to content

Commit

Permalink
Merge pull request #59 from WTFart/develop
Browse files Browse the repository at this point in the history
Finish CIDR Calculator Application
  • Loading branch information
mickeycj authored Nov 11, 2017
2 parents f3a77e3 + ee2dc04 commit 0d2e9c4
Show file tree
Hide file tree
Showing 31 changed files with 2,653 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ captures/
.idea/libraries
.idea/misc.xml
.idea/vcs.xml
.idea/inspectionProfiles/

# Keystore files
# Uncomment the following line if you do not want to check your keystore files in.
Expand Down
33 changes: 33 additions & 0 deletions .travis.yml
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
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.wtfart.ipaddressmanager"
minSdkVersion 19
Expand All @@ -24,9 +25,8 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".EmptyActivity">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustPan">
</activity>
</application>

</manifest>
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 app/src/main/java/com/wtfart/ipaddressmanager/DetailFragment.kt
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 app/src/main/java/com/wtfart/ipaddressmanager/EmptyActivity.kt

This file was deleted.

64 changes: 64 additions & 0 deletions app/src/main/java/com/wtfart/ipaddressmanager/MainActivity.kt
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 app/src/main/java/com/wtfart/ipaddressmanager/SplashActivity.kt
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)
}
}
Loading

0 comments on commit 0d2e9c4

Please sign in to comment.