diff --git a/build.gradle b/build.gradle index cdaec8c..430b28f 100644 --- a/build.gradle +++ b/build.gradle @@ -43,6 +43,7 @@ ext { } buildscript { + ext.kotlin_version = '1.2.10' repositories { google() jcenter() @@ -52,6 +53,7 @@ buildscript { classpath 'com.android.tools.build:gradle:3.0.1' classpath 'com.novoda:bintray-release:0.8.0' classpath 'pt.simdea.verifier:verifier:3.6.0' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } diff --git a/docs/adal-accounts.md b/docs/adal-accounts.md index a6f57e3..82a3bb1 100644 --- a/docs/adal-accounts.md +++ b/docs/adal-accounts.md @@ -15,6 +15,7 @@ dependencies { ``` ### Usage +#### Java ```java public class FragmentAccounts extends BaseFragment { @@ -49,6 +50,37 @@ public class FragmentAccounts extends BaseFragment { } } ``` +#### Kotlin +```kotlin +class FragmentAccounts : BaseFragment() { + + override fun doOnCreated() { + AccountHelper.initialize(getActivity()) + + getAccount() + addAccount() + clearAccount() + } + + private fun clearAccount() { + AccountHelper.clearAccounts(context) { } + } + + private fun addAccount() { + AccountHelper.addAccount(getContext(), "hardcoded_name", "hardcoded_password", "hardcoded_token") + } + + private fun getAccount() { + val account = AccountHelper.getCurrentAccount(context) + if (account != null) { + Toast.makeText(getContext(), "Name: " + account.name + " \nPassword: " + AccountHelper.getAccountPassword(account) + " \ntoken: " + AccountHelper.getCurrentToken(account, getContext()), Toast.LENGTH_LONG).show() + } else { + Toast.makeText(getContext(), "No account available" , Toast.LENGTH_LONG).show() + } + } +} +``` + ### Contributing [CONTRIBUTING](../CONTRIBUTING.md) diff --git a/docs/adal-adapters.md b/docs/adal-adapters.md index a4ef4c5..9141140 100644 --- a/docs/adal-adapters.md +++ b/docs/adal-adapters.md @@ -16,7 +16,7 @@ dependencies { } ``` ### Usage - +#### Java Create a new adapter: ```java public class AdapterPhoto extends AbstractLoadMoreBaseAdapter { @@ -62,6 +62,41 @@ mAdapterPost.setOnLoadMoreListener(new OnLoadMoreListener() { }); ``` +#### Kotlin +Create a new adapter: +```kotlin +class AdapterPhoto : AbstractLoadMoreBaseAdapter(R.layout.adapter_photo, R.layout.adapter_loading, ArrayList()) { + + override fun bindItem(holder: BaseViewHolder, item: Photo) { + val imgThumbnail = holder.getView(R.id.imgThumbnail) + + Glide.with(imgThumbnail.context).load(item.mThumbnailUrl).into(imgThumbnail) + holder.setText(R.id.txtTitle, item.mTitle) + holder.setText(R.id.txtDescription, item.mTitle) + } + + override fun bindError(holder: BaseViewHolder, loadingError: Boolean) { + val lnlLoading = holder.getView(R.id.lnrLoading) + val lnlError = holder.getView(R.id.lnrError) + + if (loadingError) { + lnlLoading.visibility = View.GONE + lnlError.visibility = View.VISIBLE + } + else { + lnlLoading.visibility = View.VISIBLE + lnlError.visibility = View.GONE + } + } +} +``` + +Set the adapter: +```kotlin +mAdapterPost = AdapterPost() +mAdapterPost!!.setOnLoadMoreListener({ request() }) +``` + ### Contributing [CONTRIBUTING](../CONTRIBUTING.md) diff --git a/docs/adal-alarm.md b/docs/adal-alarm.md index eb902ac..5d113af 100644 --- a/docs/adal-alarm.md +++ b/docs/adal-alarm.md @@ -15,7 +15,7 @@ dependencies { } ``` ### Usage - +#### Java ```java /** * Add a new alarm to the system. @@ -44,6 +44,35 @@ private void removeAlarm() { } } ``` +#### Kotlin +```kotlin +/** + * Add a new alarm to the system. + */ +private fun addAlarm() { + val calendar = Calendar.getInstance() + calendar.time = Date() + + if (!AlarmManager.hasAlarm(context, mIntentAlarm, 1001)) { + AlarmManager.addAlarm(context, mIntentAlarm, 1001, calendar) + SnackBuilder.show(mBtnRemoveAlarm, "Alarm added!", R.color.colorAccent) + } else { + SnackBuilder.show(mBtnAddAlarm, "Alarm already added.", R.color.colorAccent) + } +} + +/** + * Remove an alarm. + */ +private fun removeAlarm() { + if (AlarmManager.hasAlarm(context, mIntentAlarm, 1001)) { + AlarmManager.cancelAlarm(context, mIntentAlarm, 1001) + SnackBuilder.show(mBtnRemoveAlarm, "Alarm removed!", R.color.colorAccent) + } else { + SnackBuilder.show(mBtnRemoveAlarm, "Please, add an alarm first!", R.color.colorAccent) + } +} +``` ### Contributing [CONTRIBUTING](../CONTRIBUTING.md) diff --git a/docs/adal-analytics.md b/docs/adal-analytics.md index de4877c..4451046 100644 --- a/docs/adal-analytics.md +++ b/docs/adal-analytics.md @@ -18,7 +18,9 @@ dependencies { // ADD THIS AT THE BOTTOM apply plugin: 'com.google.gms.google-services' ``` -### Usage with Google Analytics +### Usage +#### Java +##### Usage with Google Analytics ```java public class FragmentAnalytics extends BaseFragment { @Override @@ -40,7 +42,7 @@ public class FragmentAnalytics extends BaseFragment { .setVariant("Black") .setPosition(1) .setCustomDimension(1, "Member"); - AnalyticsManager.with(getContext()).sendProduct(product, "Search Results", "searchResults"); + AnalyticsManager.with(getContext()).sendImpression(product, "Search Results", "searchResults"); // Send product action (product can be null in this case) ProductAction productAction = new ProductAction(ProductAction.ACTION_CLICK) @@ -61,7 +63,7 @@ public class FragmentAnalytics extends BaseFragment { ... } ``` -### Usage with Firebase Analytics +##### Usage with Firebase Analytics ```java public class FragmentFirebaseAnalytics extends BaseFragment { @Override @@ -82,6 +84,67 @@ public class FragmentFirebaseAnalytics extends BaseFragment { ... } ``` + +#### Kotlin +##### Usage with Google Analytics +```kotlin +class FragmentAnalytics : BaseFragment() { + override fun doOnCreated() { + // Send a screen to GA + AnalyticsManager.with(context!!).sendScreen("FragmentAnalytics") + + // Send Enhanced Ecommerce actions to GA + // Send impression + val product = Product() + .setId("P12345") + .setName("Android Warhol T-Shirt") + .setCategory("Apparel/T-Shirts") + .setBrand("Google") + .setVariant("Black") + .setPosition(1) + .setCustomDimension(1, "Member") + AnalyticsManager.with(context!!).sendImpression(product, "Search Results", "searchResults") + + // Send product action (product can be null in this case) + val productAction = ProductAction(ProductAction.ACTION_CLICK) + .setProductActionList("Search Results") + AnalyticsManager.with(context!!).sendProduct(productAction, product, null, "searchResults") + + // Send promotion + val promotion = Promotion() + .setId("PROMO_1234") + .setName("Summer Sale") + .setCreative("summer_banner2") + .setPosition("banner_slot1") + AnalyticsManager.with(context!!).sendPromotion(promotion, "promotions") + + // Send promotion action + AnalyticsManager.with(context!!).sendPromotionAction(promotion, Promotion.ACTION_CLICK, "Internal Promotions", "click", "Summer Sale", "promotions") + } + ... +} +``` +##### Usage with Firebase Analytics +```kotlin +class FragmentFirebaseAnalytics : BaseFragment() { + override fun doOnCreated() { + // Send a screen to FA + FirebaseAnalyticsManager.sendScreen(activity, R.string.analytics_screen_main) + + // Send an event to FA + val hashMap = HashMap() + hashMap.put("event_category", "Category") + hashMap.put("event_action", "Action") + hashMap.put("event_label", "Label") + FirebaseAnalyticsManager.sendEvent(activity, "EventName", hashMap) + + // Send a user property to FA + FirebaseAnalyticsManager.sendUserProperty(activity, "Property", "Value") + } + ... +} +``` + ### Contributing [CONTRIBUTING](../CONTRIBUTING.md) diff --git a/docs/adal-application-state.md b/docs/adal-application-state.md index 643809d..7d9d870 100644 --- a/docs/adal-application-state.md +++ b/docs/adal-application-state.md @@ -15,6 +15,7 @@ dependencies { ``` ### Usage +#### Java ```java public class App extends Application implements ApplicationStateManager.BackAndForegroundListener { @@ -64,6 +65,47 @@ application.registerActivityLifecycleCallbacks(new ApplicationStateManager() { }); ``` +#### Kotlin +```kotlin +class App : Application(), ApplicationStateManager.BackAndForegroundListener { + + var TAG: String = App::class.simpleName!! + private var mApplicationStateManager: ApplicationStateManager? = null + + override fun onCreate() { + super.onCreate() + mApplicationStateManager = ApplicationStateManager(this) + registerActivityLifecycleCallbacks(mApplicationStateManager) + } + + override fun onBackground() { + Log.d(TAG, "onBackground") + } + + override fun onForeground() { + Log.d(TAG, "onForeground") + } + + fun isBackground(): Boolean = mApplicationStateManager!!.isBackground + + fun isForeground(): Boolean = mApplicationStateManager!!.isForeground +} +``` + +Another way of using `ApplicationStateManager` with a `context` reference. +```kotlin +val application = context.getApplicationContext() as Application +application.registerActivityLifecycleCallbacks(object : ApplicationStateManager() { + override fun onActivityResumed(activity: Activity) { + doOnActivityResumed(activity) + } + + override fun onActivityPaused(activity: Activity) { + doOnActivityPaused(activity) + } +}) +``` + ### Contributing [CONTRIBUTING](../CONTRIBUTING.md) diff --git a/docs/adal-bus.md b/docs/adal-bus.md index 23190a4..219d2b6 100644 --- a/docs/adal-bus.md +++ b/docs/adal-bus.md @@ -15,6 +15,7 @@ dependencies { ``` ### Usage +#### Java * Subscriber ```java public static final String BANG_A = "BANG_A"; @@ -60,6 +61,54 @@ BangBus.with(getContext()) .bang(); ``` +#### Kotlin +* Subscriber +```kotlin +companion object { + val BANG_A: String = "BANG_A" +} +private var mBangBus: BangBus? = null + +// Initialize +mBangBus = BangBus(activity) + +// Subscribe with Action +@BangBus.SubscribeBang(action = "BANG_A") +fun bangWithAction(message: String) { + mTxtResult!!.text = message +} + +// Subscribe Without Action +@BangBus.SubscribeBang +fun bangWithoutAction(number: Int?) { + mTxtResult!!.text = number.toString() +} + +// Unsubscribe +@Override +override fun onDestroy() { + mBangBus!!.unsubscribe() + super.onDestroy() +} +``` + +* Sender +```kotlin +val BANG_MESSAGE_WITH_ACTION = "received bang with action" +val BANG_NUMBER_WITHOUT_NUMBER = 666 + +// Send with Action +BangBus.with(context) + .addAction(FragmentA.BANG_A) + .setParameter(BANG_MESSAGE_WITH_ACTION) + .bang() + +// Send Bang Without Action +BangBus.with(context) + .setParameter(BANG_NUMBER_WITHOUT_NUMBER) + .bang() +``` + ### Contributing [CONTRIBUTING](../CONTRIBUTING.md) diff --git a/docs/adal-connectivity.md b/docs/adal-connectivity.md index b0716bc..83cc22b 100644 --- a/docs/adal-connectivity.md +++ b/docs/adal-connectivity.md @@ -11,7 +11,7 @@ dependencies { } ``` ### Usage - +#### Java ```java /** * Connectivity Change Fragment meant to test the {@link ConnectionChangeReceiver} for connectivity changes. @@ -110,7 +110,105 @@ public class FragmentConnectivityAware extends BaseFragment implements View.OnCl } } +``` + +#### Kotlin +```kotlin +/** + * Connectivity Change Fragment meant to test the {@link ConnectionChangeReceiver} for connectivity changes. + */ +class FragmentConnectivityAware : BaseFragment(), View.OnClickListener { + + private var mTxtMessage: TextView? = null + private var mCheckConnectivityButton: Button? = null + + /** + * Network verification. + */ + private val mConnectionReceiver = object : ConnectionChangeReceiver() { + override fun onReceive(context: Context, intent: Intent) { + if (ConnectionChangeReceiver.CONNECTIVITY_CHANGE_FILTER == intent.action) { + checkConnectivity() + } + } + } + + override fun getFromBundle(bundle: Bundle) { + // Intended. + } + + override fun layoutToInflate(): Int = R.layout.fragment_connectivity_aware + + override fun restoreInstanceState(savedInstanceState: Bundle?) { + // Intended. + } + + override fun doOnCreated() { + activity!!.setTitle(R.string.sample_network) + mTxtMessage = findViewById(R.id.txtMessage) + mCheckConnectivityButton = findViewById(R.id.btnCheckConnectivity) + } + + /** + * Called when FragmentConnectivityAware is about to become visible. + */ + override fun onStart() { + super.onStart() + mConnectionReceiver.registerConnectionChangeReceiver(activity) + mCheckConnectivityButton!!.setOnClickListener(this) + } + + /** + * Called when the FragmentConnectivityAware has become visible. + */ + override fun onResume() { + super.onResume() + checkConnectivity() + } + + /** + * Called just before the FragmentConnectivityAware is destroyed. + */ + override fun onDestroy() { + mConnectionReceiver?.unregisterConnectionChangeReceiver() + super.onDestroy() + } + + /** + * Called when a view has been clicked. + * @param view the clicked [View]. + */ + override fun onClick(view: View) { + if (R.id.btnCheckConnectivity == view.id) { + checkConnectivity() + } + } + /** + * Check whether the device has Internet connectivity or not. + */ + private fun checkConnectivity() { + if (isVisible) { + val isOnline = NetworkUtils.isNetworkConnected(activity) + handleConnectivityStatusChange(isOnline) + } + } + + /** + * Log the device Internet connectivity status. + * + * @param isOnline boolean value indicating whether the device has a connection established or not. + */ + private fun handleConnectivityStatusChange(isOnline: Boolean) { + val deviceConnectivity = if (isOnline) + getString(R.string.connectivity_device_online) + else + getString(R.string.connectivity_device_offline) + mTxtMessage!!.text = deviceConnectivity + Toast.makeText(activity, deviceConnectivity, Toast.LENGTH_SHORT).show() + } + +} ``` ### Contributing diff --git a/docs/adal-dialogs.md b/docs/adal-dialogs.md index 4923628..3cd6f86 100644 --- a/docs/adal-dialogs.md +++ b/docs/adal-dialogs.md @@ -14,7 +14,7 @@ dependencies { } ``` ### Usage - +#### Java BaseDialog: ```java public class DialogTest extends BaseDialog { @@ -61,6 +61,32 @@ public class DialogFragmentTest extends BaseDialogFragment { } ``` +#### Kotlin +BaseDialog: +```kotlin +class DialogTest(context: Context) : BaseDialog(context) { + + override fun layoutToInflate(): Int = R.layout.dialog_test + + override fun doOnCreated() { + findViewById(R.id.btnDismiss)!!.setOnClickListener({ dismiss() }) + } + +} +``` + +BaseDialogFragment: +```kotlin +class DialogFragmentTest : BaseDialogFragment() { + + override fun layoutToInflate(): Int = R.layout.dialog_test + + override fun doOnCreated() { + findViewById(R.id.btnDismiss)!!.setOnClickListener { dismiss() } + } +} +``` + ### Contributing [CONTRIBUTING](../CONTRIBUTING.md) diff --git a/docs/adal-fragments.md b/docs/adal-fragments.md index 07b0e0c..4ff8060 100644 --- a/docs/adal-fragments.md +++ b/docs/adal-fragments.md @@ -13,7 +13,7 @@ dependencies { } ``` ### Usage - +#### Java BaseFragment: ```java public class FragmentA extends BaseFragment { @@ -53,7 +53,6 @@ public class FragmentSplash extends AbstractSplashFragment { }; } } - ``` AbstractRequestFragment: @@ -70,7 +69,48 @@ public class FragmentRequest extends AbstractRequestFragment { addRequest(...); } } +``` + +#### Kotlin +BaseFragment: +```kotlin +class FragmentA : BaseFragment() { + override fun layoutToInflate(): Int = R.layout.fragment_a + + override fun doOnCreated() { + activity!!.setTitle(R.string.sample_bangbus) + } +} +``` + +AbstractSplashFragment: +```kotlin +class FragmentSplash : AbstractSplashFragment() { + + override fun layoutToInflate(): Int = R.layout.fragment_splash_screen + override fun onSplashStarted() { + onSplashFinish(openHome()) + } + + private fun openHome(): AbstractSplashFragment.OnFinishSplashScreen = OnFinishSplashScreen { + ActivityCall.init(activity!!, ActivityToolbar::class, FragmentHome::class) + .build() + activity!!.finish() + } +} +``` + +AbstractRequestFragment: +```kotlin +class FragmentRequest : AbstractRequestFragment() { + + override fun layoutToInflate(): Int = R.layout.fragment_network_request + + override fun doOnCreated() { + addRequest(...) + } +} ``` ### Contributing diff --git a/docs/adal-location.md b/docs/adal-location.md index bd9226c..8d31d29 100644 --- a/docs/adal-location.md +++ b/docs/adal-location.md @@ -15,6 +15,7 @@ dependencies { ``` ### Usage +#### Java ```java public class FragmentLocation extends BaseFragment { @@ -127,6 +128,107 @@ public class FragmentLocation extends BaseFragment { } ``` +#### Kotlin +```kotlin +class FragmentLocation : BaseFragment() { + private var mLocationManager: LocationManager? = null + + override fun doOnCreated() { + getLocation() + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + mLocationManager = LocationManager() + mLocationManager!!.onCreate(this) + } + + override fun onDestroy() { + mLocationManager!!.onDestroy() + super.onDestroy() + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + mLocationManager!!.onRequestPermissionsResult(requestCode, permissions, *grantResults) + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { + super.onActivityResult(requestCode, resultCode, data) + mLocationManager!!.onActivityResult(requestCode, resultCode) + } + + private fun getLocation() { + mLocationManager!!.requestSingleLocation(true, object : OnLocationManager { + + override fun onLocationFound(location: Location, isLastKnowLocation: Boolean) { + // Use location here. + } + + override fun onLocationError(locationError: LocationError) { + when (locationError) { + LocationError.DISABLED -> {} + LocationError.TIMEOUT -> {} + else -> {} + } + } + + override fun onPermissionsDenied() { + // Permissions denied. + } + + override fun onProviderEnabled() { + // Provider enabled. + } + + override fun onProviderDisabled() { + // Provider disabled. + } + + override fun onStopRequestUpdate() { + + } + }) + } + + private fun getLocationUpdates() { + mLocationManager!!.requestLocationUpdates(object : OnLocationManager { + + override fun onLocationFound(location: Location, isLastKnowLocation: Boolean) { + // Use location here. + } + + override fun onLocationError(locationError: LocationError) { + when (locationError) { + LocationError.DISABLED -> {} + LocationError.TIMEOUT -> {} + else -> {} + } + } + + override fun onPermissionsDenied() { + // Permissions denied. + } + + override fun onProviderEnabled() { + // Provider enabled. + } + + override fun onProviderDisabled() { + // Provider disabled. + } + + override fun onStopRequestUpdate() { + + } + }) + } + + private fun stopLocationUpdates() { + mLocationManager!!.stopRequestLocation(); + } +} +``` + ### Contributing [CONTRIBUTING](../CONTRIBUTING.md) diff --git a/docs/adal-network.md b/docs/adal-network.md index 3595753..1f91c51 100644 --- a/docs/adal-network.md +++ b/docs/adal-network.md @@ -15,6 +15,7 @@ dependencies { } ``` ### Usage +#### Java ```java public class FragmentNetworkRequest extends AbstractRequestFragment { @@ -58,6 +59,44 @@ public class FragmentNetworkRequest extends AbstractRequestFragment { } } ``` + +#### Kotlin +```kotlin +class FragmentNetworkRequest : AbstractRequestFragment() { + + private var mRclItems: RecyclerView? = null + private var mAdapterPost: AdapterPost? = null + + override fun doOnCreated() { + mRclItems!!.layoutManager = LinearLayoutManager(context) + + mAdapterPost = AdapterPost() + mAdapterPost!!.setOnLoadMoreListener({ request() }) + mRclItems!!.adapter = mAdapterPost + + request() + } + + private fun request() { + + // Show the general loading if the adapter is empty + if (mAdapterPost!!.isEmpty) { + showLoading() + } + + addRequest(APIRequests.getPosts(object : APIRequestCallback>(context) { + override fun onSuccess(posts: ResponseList) { + mAdapterPost!!.addAll(posts) + } + + override fun onError(error: APIError, isServerError: Boolean) { + showError(error.message) + } + })) + } +} +``` + ### Contributing [CONTRIBUTING](../CONTRIBUTING.md) diff --git a/docs/adal-permissions.md b/docs/adal-permissions.md index 21bdf37..cc60174 100644 --- a/docs/adal-permissions.md +++ b/docs/adal-permissions.md @@ -16,6 +16,7 @@ dependencies { ``` ### Usage +#### Java ```java public class FragmentPermissions extends BaseFragment { @@ -52,8 +53,41 @@ public class FragmentPermissions extends BaseFragment { mPermissionsManager.onPermissionResult(requestCode); } } +``` + +#### Kotlin +```kotlin +class FragmentPermissions : BaseFragment() { + + private var mPermissionsManager: PermissionsManager? = null + + override fun doOnCreated() { + mPermissionsManager = PermissionsManager(this) + requestPermissions() + } + + private fun requestPermissions() { + mPermissionsManager!!.requestPermissions(object : PermissionsManager.OnPermissionsListener { + override fun onGranted() { + // Granted. + } + + override fun onDenied(neverAskMeAgain: Boolean) { + // Denied. + if (neverAskMeAgain) { + // User clicked nerver ask me again. + } + } + }, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WRITE_CALENDAR) + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + mPermissionsManager!!.onPermissionResult(requestCode) + } +} ``` + ### Contributing [CONTRIBUTING](../CONTRIBUTING.md) diff --git a/docs/adal-utils.md b/docs/adal-utils.md index 5eb363d..40b42da 100644 --- a/docs/adal-utils.md +++ b/docs/adal-utils.md @@ -19,6 +19,7 @@ dependencies { } ``` ### Usage +#### Java ```java // Open settings AppUtils.openAppSettings(getActivity()); @@ -33,6 +34,21 @@ AppUtils.openDial(getActivity(), "00351910000000"); AppUtils.openEmail(getActivity(), getString(R.string.send_email), "teste@teste.com", "teste2@teste.com"); ``` +#### Kotlin +```kotlin +// Open settings +AppUtils.openAppSettings(activity) + +// Check if Google Play Services exists +AppUtils.checkPlayServicesExists(activity) + +// Open Dial +AppUtils.openDial(activity, "00351910000000") + +// Open Email +AppUtils.openEmail(activity, getString(R.string.send_email), "teste@teste.com", "teste2@teste.com") +``` + ### Contributing [CONTRIBUTING](../CONTRIBUTING.md) diff --git a/sample-kotlin/.gitignore b/sample-kotlin/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/sample-kotlin/.gitignore @@ -0,0 +1 @@ +/build diff --git a/sample-kotlin/build.gradle b/sample-kotlin/build.gradle new file mode 100644 index 0000000..33ab548 --- /dev/null +++ b/sample-kotlin/build.gradle @@ -0,0 +1,65 @@ +/* + * ADAL - A set of Android libraries to help speed up Android development. + * + * Copyright (c) 2018 ADAL + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/** + * Added for adal-analytics + */ +buildscript { + repositories { + jcenter() + } + dependencies { + classpath 'com.google.gms:google-services:3.1.0' + } +} + +apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' + +android { + compileSdkVersion project.compileSdkVersion.toInteger() + buildToolsVersion project.buildToolsVersion + + defaultConfig { + minSdkVersion project.minSdkVersion.toInteger() + targetSdkVersion project.targetSdkVersion.toInteger() + buildConfigField "String", "API_BASE_URL", "\"https://jsonplaceholder.typicode.com/\"" + buildConfigField "long", "API_TIMEOUT", "30" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + + lintOptions { + disable 'InvalidPackage' + } +} + + +apply from: 'dependencies.gradle' + +/** + * Added for adal-analytics + */ +apply plugin: 'com.google.gms.google-services' \ No newline at end of file diff --git a/sample-kotlin/dependencies.gradle b/sample-kotlin/dependencies.gradle new file mode 100644 index 0000000..13465cf --- /dev/null +++ b/sample-kotlin/dependencies.gradle @@ -0,0 +1,58 @@ +/* + * ADAL - A set of Android libraries to help speed up Android development. + * + * Copyright (c) 2018 ADAL + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +dependencies { + + implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" + /** APP **/ + + /* ACTIVITY FRAGMENT MANAGER */ + implementation "com.massivedisaster:afm:0.0.1" + + /* ADAL */ + implementation project(':adal') + + /* SUPPORT */ + implementation "com.android.support:appcompat-v7:$project.supportVersion" + implementation "com.android.support:design:$project.supportVersion" + implementation 'com.android.support.constraint:constraint-layout:1.0.2' + + /* GLIDE */ + implementation "com.github.bumptech.glide:glide:$project.glideVersion" + + /** TEST **/ + + /* ESPRESSO */ + androidTestImplementation("com.android.support.test.espresso:espresso-core:$project.espressoVersion") { + exclude group: 'com.android.support', module: 'support-annotations' + } + + /* JUNIT */ + testImplementation "junit:junit:$project.jUnitVersion" + + /* MOCKITO */ + androidTestImplementation "org.mockito:mockito-android:$project.mockitoVersion" + testImplementation "org.mockito:mockito-core:$project.mockitoVersion" +} diff --git a/sample-kotlin/google-services.json b/sample-kotlin/google-services.json new file mode 100644 index 0000000..dc91a9f --- /dev/null +++ b/sample-kotlin/google-services.json @@ -0,0 +1,42 @@ +{ + "project_info": { + "project_number": "698332537945", + "firebase_url": "https://adalsample.firebaseio.com", + "project_id": "adalsample", + "storage_bucket": "adalsample.appspot.com" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:698332537945:android:d2eca8bc104bd788", + "android_client_info": { + "package_name": "com.massivedisaster.adal.samplekotlin" + } + }, + "oauth_client": [ + { + "client_id": "698332537945-kngktebjh1g9g6k3e1icun5696c25esg.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyBG29B6lWwts1zm3BZoeyL7fiQfaRb5pUI" + } + ], + "services": { + "analytics_service": { + "status": 1 + }, + "appinvite_service": { + "status": 1, + "other_platform_oauth_client": [] + }, + "ads_service": { + "status": 2 + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/sample-kotlin/proguard-rules.pro b/sample-kotlin/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/sample-kotlin/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/sample-kotlin/src/dev/res/drawable-xxxhdpi/ic_label.png b/sample-kotlin/src/dev/res/drawable-xxxhdpi/ic_label.png new file mode 100644 index 0000000..ef4fa27 Binary files /dev/null and b/sample-kotlin/src/dev/res/drawable-xxxhdpi/ic_label.png differ diff --git a/sample-kotlin/src/dev/res/drawable/ic_launcher.xml b/sample-kotlin/src/dev/res/drawable/ic_launcher.xml new file mode 100644 index 0000000..536423d --- /dev/null +++ b/sample-kotlin/src/dev/res/drawable/ic_launcher.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/sample-kotlin/src/main/AndroidManifest.xml b/sample-kotlin/src/main/AndroidManifest.xml new file mode 100644 index 0000000..0a22e1c --- /dev/null +++ b/sample-kotlin/src/main/AndroidManifest.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/app/App.kt b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/app/App.kt new file mode 100644 index 0000000..4868c35 --- /dev/null +++ b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/app/App.kt @@ -0,0 +1,55 @@ +/* + * ADAL - A set of Android libraries to help speed up Android development. + * + * Copyright (c) 2018 ADAL + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.massivedisaster.adal.samplekotlin.app + +import android.app.Application +import android.widget.Toast +import com.massivedisaster.adal.applicationstate.ApplicationStateManager + +class App : Application(), ApplicationStateManager.BackAndForegroundListener { + + var TAG: String = App::class.simpleName!! + + private var mApplicationStateManager: ApplicationStateManager? = null + + override fun onCreate() { + super.onCreate() + mApplicationStateManager = ApplicationStateManager(this) + registerActivityLifecycleCallbacks(mApplicationStateManager) + } + + override fun onBackground() { + Toast.makeText(this, "onBackground called", Toast.LENGTH_SHORT).show() + } + + override fun onForeground() { + Toast.makeText(this, "onForeground called", Toast.LENGTH_SHORT).show() + } + + fun isBackground(): Boolean = mApplicationStateManager!!.isBackground + + fun isForeground(): Boolean = mApplicationStateManager!!.isForeground +} \ No newline at end of file diff --git a/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/base/activity/ActivityFullScreen.kt b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/base/activity/ActivityFullScreen.kt new file mode 100644 index 0000000..135adae --- /dev/null +++ b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/base/activity/ActivityFullScreen.kt @@ -0,0 +1,35 @@ +/* + * ADAL - A set of Android libraries to help speed up Android development. + * + * Copyright (c) 2018 ADAL + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.massivedisaster.adal.samplekotlin.base.activity + +import com.massivedisaster.adal.samplekotlin.R +import com.massivedisaster.afm.activity.BaseActivity + +open class ActivityFullScreen : BaseActivity() { + override fun getContainerViewId(): Int = R.id.frmContainer + + override fun layoutToInflate(): Int = R.layout.activity_fullscreen +} diff --git a/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/base/activity/ActivityToolbar.kt b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/base/activity/ActivityToolbar.kt new file mode 100644 index 0000000..d35e516 --- /dev/null +++ b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/base/activity/ActivityToolbar.kt @@ -0,0 +1,59 @@ +/* + * ADAL - A set of Android libraries to help speed up Android development. + * + * Copyright (c) 2018 ADAL + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.massivedisaster.adal.samplekotlin.base.activity + +import android.os.Bundle +import android.support.v7.widget.Toolbar +import android.view.MenuItem +import com.massivedisaster.adal.samplekotlin.R +import com.massivedisaster.afm.activity.BaseActivity + +class ActivityToolbar : BaseActivity() { + override fun layoutToInflate(): Int = R.layout.activity_toolbar + + private var mToolbar: Toolbar? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + mToolbar = findViewById(R.id.toolbar) + setSupportActionBar(mToolbar) + + if (supportActionBar != null) { + supportActionBar!!.setDisplayHomeAsUpEnabled(true) + } + } + + override fun getContainerViewId(): Int = R.id.frmContainer + + override fun onOptionsItemSelected(item: MenuItem?): Boolean = when (item?.itemId) { + android.R.id.home -> { + onBackPressed() + true + } + else -> super.onOptionsItemSelected(item) + } +} \ No newline at end of file diff --git a/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/accounts/FragmentAccounts.kt b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/accounts/FragmentAccounts.kt new file mode 100644 index 0000000..621a619 --- /dev/null +++ b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/accounts/FragmentAccounts.kt @@ -0,0 +1,89 @@ +/* + * ADAL - A set of Android libraries to help speed up Android development. + * + * Copyright (c) 2018 ADAL + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.massivedisaster.adal.samplekotlin.feature.accounts + +import android.os.Bundle +import android.widget.Button +import android.widget.Toast +import com.massivedisaster.adal.account.AccountHelper +import com.massivedisaster.adal.fragment.BaseFragment +import com.massivedisaster.adal.samplekotlin.R +import com.massivedisaster.adal.utils.SnackBuilder + +class FragmentAccounts : BaseFragment() { + + private var mBtnGetAccount: Button? = null + private var mBtnAddHardCodedAccount: Button? = null + private var mBtnClearAccount: Button? = null + + override fun getFromBundle(bundle: Bundle) { + // Intended. + } + + override fun layoutToInflate(): Int = R.layout.fragment_accounts + + override fun restoreInstanceState(savedInstanceState: Bundle?) { + // Intended. + } + + override fun doOnCreated() { + activity!!.setTitle(R.string.sample_accounts) + + mBtnGetAccount = findViewById(R.id.btnGetAccount) + mBtnAddHardCodedAccount = findViewById(R.id.btnAddHardCodedAccount) + mBtnClearAccount = findViewById(R.id.btnClearAccount) + + initialize() + } + + private fun initialize() { + AccountHelper.initialize(activity) + + mBtnGetAccount!!.setOnClickListener { getAccount() } + + mBtnAddHardCodedAccount!!.setOnClickListener({ addAccount() }) + + mBtnClearAccount!!.setOnClickListener({ clearAccount() }) + } + + private fun clearAccount() { + AccountHelper.clearAccounts(context) { SnackBuilder.show(mBtnGetAccount, "Clear account finished.", R.color.colorAccent) } + } + + private fun addAccount() { + AccountHelper.addAccount(context, "hardcoded_name", "hardcoded_password", "hardcoded_token") + SnackBuilder.show(mBtnGetAccount, "Added account.", R.color.colorAccent) + } + + private fun getAccount() { + val account = AccountHelper.getCurrentAccount(context) + if (account != null) { + Toast.makeText(context, "Name: " + account.name + " \nPassword: " + AccountHelper.getAccountPassword(account) + " \ntoken: " + AccountHelper.getCurrentToken(account, context), Toast.LENGTH_LONG).show() + } else { + SnackBuilder.show(mBtnGetAccount, "No accounts", R.color.colorAccent) + } + } +} \ No newline at end of file diff --git a/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/alarm/AlarmReceiver.kt b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/alarm/AlarmReceiver.kt new file mode 100644 index 0000000..764cc85 --- /dev/null +++ b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/alarm/AlarmReceiver.kt @@ -0,0 +1,66 @@ +/* + * ADAL - A set of Android libraries to help speed up Android development. + * + * Copyright (c) 2018 ADAL + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.massivedisaster.adal.samplekotlin.feature.alarm + +import android.R.attr.id +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.media.RingtoneManager +import android.support.v4.app.NotificationCompat +import com.massivedisaster.adal.samplekotlin.R +import com.massivedisaster.adal.samplekotlin.base.activity.ActivityFullScreen + +class AlarmReceiver : BroadcastReceiver() { + + override fun onReceive(context: Context, intent: Intent) { + val i = Intent(context, ActivityFullScreen::class.java) + i.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP + i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + + createNotification(context, i, id, intent.getStringExtra(FragmentAlarm::PARAM_EVENT_NAME.toString())) + } + + private fun createNotification(context: Context, intent: Intent, requestCode: Int, message: String) { + val pendingIntent = PendingIntent.getActivity(context, requestCode, intent, + PendingIntent.FLAG_ONE_SHOT) + + val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) + val notificationBuilder = NotificationCompat.Builder(context) + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle(context.getString(R.string.app_name)) + .setContentText(message) + .setAutoCancel(true) + .setSound(defaultSoundUri) + .setContentIntent(pendingIntent) + + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + + notificationManager.notify(0, notificationBuilder.build()) + } +} \ No newline at end of file diff --git a/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/alarm/FragmentAlarm.kt b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/alarm/FragmentAlarm.kt new file mode 100644 index 0000000..d2d9d5d --- /dev/null +++ b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/alarm/FragmentAlarm.kt @@ -0,0 +1,103 @@ +/* + * ADAL - A set of Android libraries to help speed up Android development. + * + * Copyright (c) 2018 ADAL + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.massivedisaster.adal.samplekotlin.feature.alarm + +import android.content.Intent +import android.os.Bundle +import android.view.View +import android.widget.Button +import com.massivedisaster.adal.alarm.AlarmManager +import com.massivedisaster.adal.fragment.BaseFragment +import com.massivedisaster.adal.samplekotlin.R +import com.massivedisaster.adal.utils.SnackBuilder +import java.util.* + +class FragmentAlarm : BaseFragment(), View.OnClickListener { + + val PARAM_EVENT_NAME: String = "PARAM_EVENT_NAME" + + private var mBtnAddAlarm: Button? = null + private var mBtnRemoveAlarm: Button? = null + private var mIntentAlarm: Intent? = null + + override fun layoutToInflate(): Int = R.layout.fragment_alarm + + override fun getFromBundle(bundle: Bundle) { + //Intended + } + + override fun restoreInstanceState(savedInstanceState: Bundle?) { + //Intended + } + + override fun doOnCreated() { + activity!!.setTitle(R.string.sample_alarm) + + mBtnAddAlarm = findViewById(R.id.btnAddAlarm) + mBtnRemoveAlarm = findViewById(R.id.btnRemoveAlarm) + + mBtnAddAlarm!!.setOnClickListener(this) + mBtnRemoveAlarm!!.setOnClickListener(this) + + mIntentAlarm = Intent(context, AlarmReceiver::class.java) + mIntentAlarm!!.putExtra(PARAM_EVENT_NAME, "Alarm message!") + } + + override fun onClick(v: View) { + when (v.id) { + R.id.btnAddAlarm -> addAlarm() + R.id.btnRemoveAlarm -> removeAlarm() + } + } + + /** + * Add a new alarm to the system. + */ + private fun addAlarm() { + val calendar = Calendar.getInstance() + calendar.time = Date() + + if (!AlarmManager.hasAlarm(context, mIntentAlarm, 1001)) { + AlarmManager.addAlarm(context, mIntentAlarm, 1001, calendar) + SnackBuilder.show(mBtnRemoveAlarm, "Alarm added!", R.color.colorAccent) + } else { + SnackBuilder.show(mBtnAddAlarm, "Alarm already added.", R.color.colorAccent) + } + } + + /** + * Remove an alarm. + */ + private fun removeAlarm() { + if (AlarmManager.hasAlarm(context, mIntentAlarm, 1001)) { + AlarmManager.cancelAlarm(context, mIntentAlarm, 1001) + SnackBuilder.show(mBtnRemoveAlarm, "Alarm removed!", R.color.colorAccent) + } else { + SnackBuilder.show(mBtnRemoveAlarm, "Please, add an alarm first!", R.color.colorAccent) + } + } + +} \ No newline at end of file diff --git a/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/analytics/FragmentAnalytics.kt b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/analytics/FragmentAnalytics.kt new file mode 100644 index 0000000..78f80e5 --- /dev/null +++ b/sample-kotlin/src/main/java/com/massivedisaster/adal/samplekotlin/feature/analytics/FragmentAnalytics.kt @@ -0,0 +1,100 @@ +/* + * ADAL - A set of Android libraries to help speed up Android development. + * + * Copyright (c) 2018 ADAL + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.massivedisaster.adal.samplekotlin.feature.analytics + +import android.support.v7.widget.AppCompatEditText +import android.text.TextUtils +import android.widget.Button +import com.massivedisaster.adal.analytics.AnalyticsManager +import com.massivedisaster.adal.fragment.BaseFragment +import com.massivedisaster.adal.samplekotlin.R +import com.massivedisaster.adal.samplekotlin.base.activity.ActivityToolbar +import com.massivedisaster.adal.utils.SnackBuilder +import com.massivedisaster.afm.ActivityCall + + +class FragmentAnalytics : BaseFragment() { + + private var mEdtAnalyticsEventLabel: AppCompatEditText? = null + + override fun layoutToInflate(): Int = R.layout.fragment_analytics + + override fun doOnCreated() { + activity!!.setTitle(R.string.sample_analytics) + + /* + * You need to generate a json file + * + * https://developers.google.com/mobile/add?platform=android&cntapi=signin&cnturl=https:%2F%2Fdevelopers.google.com%2Fidentity%2Fsign-in%2Fandroid%2Fsign-in%3Fconfigured%3Dtrue&cntlbl=Continue%20Adding%20Sign-In + */ + + /* + * Send a screen to GA + */ + AnalyticsManager.with(context!!).sendScreen(R.string.analytics_screen_main) + + val button = findViewById