From 18a57d96258ce96c94f95073ff095691364a8777 Mon Sep 17 00:00:00 2001 From: lyn Date: Sun, 23 Jul 2017 01:23:28 +0900 Subject: [PATCH 01/34] convert Marquee java to kt and apply kotlin plugin --- build.gradle | 2 ++ mobile/build.gradle | 5 +++++ .../android/apis/text/{Marquee.java => Marquee.kt} | 10 ++++------ 3 files changed, 11 insertions(+), 6 deletions(-) rename mobile/src/main/java/com/example/android/apis/text/{Marquee.java => Marquee.kt} (78%) diff --git a/build.gradle b/build.gradle index e4873fbe3..1ec0c41b0 100644 --- a/build.gradle +++ b/build.gradle @@ -2,11 +2,13 @@ // vim: ts=4 sts=4 sw=4 expandtab buildscript { + ext.kotlin_version = '1.1.3-2' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/mobile/build.gradle b/mobile/build.gradle index 413448435..0f3641797 100644 --- a/mobile/build.gradle +++ b/mobile/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' android { compileSdkVersion 26 @@ -27,4 +28,8 @@ dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:support-v4:26.0.0-beta2' compile 'com.android.support:appcompat-v7:26.0.0-beta2' + compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" +} +repositories { + mavenCentral() } diff --git a/mobile/src/main/java/com/example/android/apis/text/Marquee.java b/mobile/src/main/java/com/example/android/apis/text/Marquee.kt similarity index 78% rename from mobile/src/main/java/com/example/android/apis/text/Marquee.java rename to mobile/src/main/java/com/example/android/apis/text/Marquee.kt index ba82ed6cf..5e040ce9f 100644 --- a/mobile/src/main/java/com/example/android/apis/text/Marquee.java +++ b/mobile/src/main/java/com/example/android/apis/text/Marquee.kt @@ -21,11 +21,9 @@ import android.app.Activity; import android.os.Bundle; -public class Marquee extends Activity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.marquee); +class Marquee : Activity() { + override fun onCreate(savedInstanceState:Bundle ?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.marquee) } } From 7132a98c144ea07bc2be09c16a90b8048c585243 Mon Sep 17 00:00:00 2001 From: lyn Date: Sun, 23 Jul 2017 01:28:24 +0900 Subject: [PATCH 02/34] convert text.LogTextBox1 java to kt --- .../text/{LogTextBox1.java => LogTextBox1.kt} | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) rename mobile/src/main/java/com/example/android/apis/text/{LogTextBox1.java => LogTextBox1.kt} (61%) diff --git a/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.java b/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.kt similarity index 61% rename from mobile/src/main/java/com/example/android/apis/text/LogTextBox1.java rename to mobile/src/main/java/com/example/android/apis/text/LogTextBox1.kt index 18136296f..b482bec17 100644 --- a/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.java +++ b/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.kt @@ -28,23 +28,18 @@ * to which text is appended. * */ -public class LogTextBox1 extends Activity { - - private LogTextBox mText; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.log_text_box_1); - - mText = (LogTextBox) findViewById(R.id.text); - - Button addButton = (Button) findViewById(R.id.add); - addButton.setOnClickListener(new View.OnClickListener() { - - public void onClick(View v) { - mText.append("This is a test\n"); - } }); +class LogTextBox1 : Activity() { + + private var mText: LogTextBox? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + setContentView(R.layout.log_text_box_1) + + mText = findViewById(R.id.text) as LogTextBox + val addButton = findViewById(R.id.add) as Button + addButton.setOnClickListener { mText!!.append("This is a test\n") } } } + From 9d24dc931ade3deb193bd4493930bffc14858582 Mon Sep 17 00:00:00 2001 From: lyn Date: Sun, 23 Jul 2017 05:49:37 +0900 Subject: [PATCH 03/34] convert LogTextBox && Link java to kt --- .../android/apis/text/{Link.java => Link.kt} | 67 +++++++++---------- .../example/android/apis/text/LogTextBox.java | 64 ------------------ .../example/android/apis/text/LogTextBox.kt | 52 ++++++++++++++ 3 files changed, 85 insertions(+), 98 deletions(-) rename mobile/src/main/java/com/example/android/apis/text/{Link.java => Link.kt} (54%) delete mode 100644 mobile/src/main/java/com/example/android/apis/text/LogTextBox.java create mode 100644 mobile/src/main/java/com/example/android/apis/text/LogTextBox.kt diff --git a/mobile/src/main/java/com/example/android/apis/text/Link.java b/mobile/src/main/java/com/example/android/apis/text/Link.kt similarity index 54% rename from mobile/src/main/java/com/example/android/apis/text/Link.java rename to mobile/src/main/java/com/example/android/apis/text/Link.kt index cc3ed5b9a..14298c31b 100644 --- a/mobile/src/main/java/com/example/android/apis/text/Link.java +++ b/mobile/src/main/java/com/example/android/apis/text/Link.kt @@ -14,27 +14,27 @@ * limitations under the License. */ -package com.example.android.apis.text; +package com.example.android.apis.text -import com.example.android.apis.R; +import com.example.android.apis.R -import android.app.Activity; -import android.graphics.Typeface; -import android.os.Bundle; -import android.text.Html; -import android.text.SpannableString; -import android.text.Spanned; -import android.text.method.LinkMovementMethod; -import android.text.style.StyleSpan; -import android.text.style.URLSpan; -import android.widget.TextView; +import android.app.Activity +import android.graphics.Typeface +import android.os.Bundle +import android.text.Html +import android.text.SpannableString +import android.text.Spanned +import android.text.method.LinkMovementMethod +import android.text.style.StyleSpan +import android.text.style.URLSpan +import android.view.View +import android.widget.TextView -public class Link extends Activity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); +class Link : Activity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) - setContentView(R.layout.link); + setContentView(R.layout.link) // text1 shows the android:autoLink property, which // automatically linkifies things like URLs and phone numbers @@ -46,8 +46,8 @@ protected void onCreate(Bundle savedInstanceState) { // respond to user input. To make them active, you need to // call setMovementMethod() on the TextView object. - TextView t2 = (TextView) findViewById(R.id.text2); - t2.setMovementMethod(LinkMovementMethod.getInstance()); + val t2 = findViewById(R.id.text2) as TextView + t2.movementMethod = LinkMovementMethod.getInstance() // text3 shows creating text with links from HTML in the Java // code, rather than from a string resource. Note that for a @@ -56,29 +56,28 @@ protected void onCreate(Bundle savedInstanceState) { // illustrate how you might display text that came from a // dynamic source (eg, the network). - TextView t3 = (TextView) findViewById(R.id.text3); - t3.setText( - Html.fromHtml( + val t3 = findViewById(R.id.text3) as TextView + t3.text = Html.fromHtml( "text3: Constructed from HTML programmatically. Text with a " + - "link " + - "created in the Java source code using HTML.")); - t3.setMovementMethod(LinkMovementMethod.getInstance()); + "link " + + "created in the Java source code using HTML.") + t3.movementMethod = LinkMovementMethod.getInstance() // text4 illustrates constructing a styled string containing a // link without using HTML at all. Again, for a fixed string // you should probably be using a string resource, not a // hardcoded value. - SpannableString ss = new SpannableString( - "text4: Manually created spans. Click here to dial the phone."); + val ss = SpannableString( + "text4: Manually created spans. Click here to dial the phone.") - ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 30, - Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - ss.setSpan(new URLSpan("tel:4155551212"), 31+6, 31+10, - Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + ss.setSpan(StyleSpan(Typeface.BOLD), 0, 30, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + ss.setSpan(URLSpan("tel:4155551212"), 31 + 6, 31 + 10, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) - TextView t4 = (TextView) findViewById(R.id.text4); - t4.setText(ss); - t4.setMovementMethod(LinkMovementMethod.getInstance()); + val t4 = findViewById(R.id.text4) as TextView + t4.text = ss + t4.movementMethod = LinkMovementMethod.getInstance() } } diff --git a/mobile/src/main/java/com/example/android/apis/text/LogTextBox.java b/mobile/src/main/java/com/example/android/apis/text/LogTextBox.java deleted file mode 100644 index 09957c5bf..000000000 --- a/mobile/src/main/java/com/example/android/apis/text/LogTextBox.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * 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.example.android.apis.text; - -import android.widget.TextView; -import android.content.Context; -import android.text.method.ScrollingMovementMethod; -import android.text.method.MovementMethod; -import android.text.Editable; -import android.util.AttributeSet; - -/** - * This is a TextView that is Editable and by default scrollable, - * like EditText without a cursor. - * - *

- * XML attributes - *

- * See - * {@link android.R.styleable#TextView TextView Attributes}, - * {@link android.R.styleable#View View Attributes} - */ -public class LogTextBox extends TextView { - public LogTextBox(Context context) { - this(context, null); - } - - public LogTextBox(Context context, AttributeSet attrs) { - this(context, attrs, android.R.attr.textViewStyle); - } - - public LogTextBox(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - } - - @Override - protected MovementMethod getDefaultMovementMethod() { - return ScrollingMovementMethod.getInstance(); - } - - @Override - public Editable getText() { - return (Editable) super.getText(); - } - - @Override - public void setText(CharSequence text, BufferType type) { - super.setText(text, BufferType.EDITABLE); - } -} diff --git a/mobile/src/main/java/com/example/android/apis/text/LogTextBox.kt b/mobile/src/main/java/com/example/android/apis/text/LogTextBox.kt new file mode 100644 index 000000000..1b7a93cec --- /dev/null +++ b/mobile/src/main/java/com/example/android/apis/text/LogTextBox.kt @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * 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.example.android.apis.text + +import android.widget.TextView +import android.content.Context +import android.text.method.ScrollingMovementMethod +import android.text.method.MovementMethod +import android.text.Editable +import android.util.AttributeSet + +/** + * This is a TextView that is Editable and by default scrollable, + * like EditText without a cursor. + + * + * + * **XML attributes** + * + * + * See + * [TextView Attributes][android.R.styleable.TextView], + * [View Attributes][android.R.styleable.View] + */ +class LogTextBox @JvmOverloads constructor(context: Context, attrs: AttributeSet ?= null, defStyle: Int = android.R.attr.textViewStyle) : TextView(context, attrs, defStyle) { + + override fun getDefaultMovementMethod(): MovementMethod { + return ScrollingMovementMethod.getInstance() + } + + override fun getText(): Editable { + return super.getText() as Editable + } + + override fun setText(text: CharSequence, type: TextView.BufferType) { + super.setText(text, TextView.BufferType.EDITABLE) + } +} From 92054df28217d0bdc3643c2c1ff78f64266fb550 Mon Sep 17 00:00:00 2001 From: lyn Date: Sun, 23 Jul 2017 06:02:05 +0900 Subject: [PATCH 04/34] found missing --- .../src/main/java/com/example/android/apis/text/_index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mobile/src/main/java/com/example/android/apis/text/_index.html b/mobile/src/main/java/com/example/android/apis/text/_index.html index c995a0cac..74c3cc474 100644 --- a/mobile/src/main/java/com/example/android/apis/text/_index.html +++ b/mobile/src/main/java/com/example/android/apis/text/_index.html @@ -1,8 +1,8 @@

Linkify
-
Demonstrates the Demonstrates the Linkify -class, which converts URLs in a block of text into hyperlinks.
+class, which converts URLs in a block of text into hyperlinks.
From 48bfdd1f1d7750781689d75e0d24a84268482f46 Mon Sep 17 00:00:00 2001 From: lyn Date: Sun, 23 Jul 2017 06:16:54 +0900 Subject: [PATCH 05/34] modified a depreciated Html.fromHtml() by version --- .../java/com/example/android/apis/text/Link.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/mobile/src/main/java/com/example/android/apis/text/Link.kt b/mobile/src/main/java/com/example/android/apis/text/Link.kt index 14298c31b..b7ae154d1 100644 --- a/mobile/src/main/java/com/example/android/apis/text/Link.kt +++ b/mobile/src/main/java/com/example/android/apis/text/Link.kt @@ -57,10 +57,18 @@ class Link : Activity() { // dynamic source (eg, the network). val t3 = findViewById(R.id.text3) as TextView - t3.text = Html.fromHtml( - "text3: Constructed from HTML programmatically. Text with a " + - "link " + - "created in the Java source code using HTML.") + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + t3.text = Html.fromHtml( + "text3: Constructed from HTML programmatically. Text with a " + + "link " + + "created in the Java source code using HTML.", Html.FROM_HTML_MODE_LEGACY) + } + else{ + t3.text = Html.fromHtml( + "text3: Constructed from HTML programmatically. Text with a " + + "link " + + "created in the Java source code using HTML.") + } t3.movementMethod = LinkMovementMethod.getInstance() // text4 illustrates constructing a styled string containing a From eb2138c9db58536a025a53022050a62ee9f7297f Mon Sep 17 00:00:00 2001 From: lyn Date: Sat, 29 Jul 2017 13:12:18 +0900 Subject: [PATCH 06/34] convert SeekBar java to kt --- .../apis/view/{SeekBar1.java => SeekBar1.kt} | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) rename mobile/src/main/java/com/example/android/apis/view/{SeekBar1.java => SeekBar1.kt} (62%) diff --git a/mobile/src/main/java/com/example/android/apis/view/SeekBar1.java b/mobile/src/main/java/com/example/android/apis/view/SeekBar1.kt similarity index 62% rename from mobile/src/main/java/com/example/android/apis/view/SeekBar1.java rename to mobile/src/main/java/com/example/android/apis/view/SeekBar1.kt index 65d01d4e2..f39b4f501 100644 --- a/mobile/src/main/java/com/example/android/apis/view/SeekBar1.java +++ b/mobile/src/main/java/com/example/android/apis/view/SeekBar1.kt @@ -18,6 +18,7 @@ import android.app.Activity; import android.os.Bundle; +import android.view.View import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; @@ -30,7 +31,7 @@ /** * Demonstrates how to use a seek bar */ -public class SeekBar1 extends Activity implements SeekBar.OnSeekBarChangeListener { +/*public class SeekBar1 extends Activity implements SeekBar.OnSeekBarChangeListener { SeekBar mSeekBar; TextView mProgressText; @@ -70,4 +71,35 @@ public void onStartTrackingTouch(SeekBar seekBar) { public void onStopTrackingTouch(SeekBar seekBar) { mTrackingText.setText(getString(R.string.seekbar_tracking_off)); } -} +}*/ +class SeekBar1 : Activity(), SeekBar.OnSeekBarChangeListener{ + lateinit private var mSeekBar : SeekBar; + lateinit private var mProgressText : TextView; + lateinit private var mTrackingText : TextView; + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.seekbar_1) + + mSeekBar = findViewById(R.id.seek) as SeekBar + mSeekBar.setOnSeekBarChangeListener(this) + mProgressText = findViewById(R.id.progress) as TextView + mTrackingText = findViewById(R.id.tracking) as TextView + + (findViewById(R.id.enabled) as CheckBox).setOnCheckedChangeListener { buttonView, isChecked -> + findViewById(R.id.seekMin).setEnabled(isChecked); + findViewById(R.id.seekMax).setEnabled(isChecked); + mSeekBar.setEnabled(isChecked); + } + } + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromTouch: Boolean) { + mProgressText.setText("$progress ${resources.getString(R.string.seekbar_from_touch)} = $fromTouch"); + } + + override fun onStartTrackingTouch(p0: SeekBar?) { + mTrackingText.setText(getString(R.string.seekbar_tracking_on)); + } + + override fun onStopTrackingTouch(p0: SeekBar?) { + mTrackingText.setText(getString(R.string.seekbar_tracking_off)); + } +} \ No newline at end of file From 2d22faca14a5bfcbdabe5bb89992f645bd6aa492 Mon Sep 17 00:00:00 2001 From: lyn Date: Sun, 30 Jul 2017 08:55:09 +0900 Subject: [PATCH 07/34] converted AlarmController java to kt --- .../android/apis/app/AlarmController.java | 168 ------------ .../android/apis/app/AlarmController.kt | 251 ++++++++++++++++++ 2 files changed, 251 insertions(+), 168 deletions(-) delete mode 100644 mobile/src/main/java/com/example/android/apis/app/AlarmController.java create mode 100644 mobile/src/main/java/com/example/android/apis/app/AlarmController.kt diff --git a/mobile/src/main/java/com/example/android/apis/app/AlarmController.java b/mobile/src/main/java/com/example/android/apis/app/AlarmController.java deleted file mode 100644 index 2ba573564..000000000 --- a/mobile/src/main/java/com/example/android/apis/app/AlarmController.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * 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.example.android.apis.app; - -// Need the following import to get access to the app resources, since this -// class is in a sub-package. -import com.example.android.apis.R; - -import android.app.Activity; -import android.app.AlarmManager; -import android.app.PendingIntent; -import android.content.Intent; -import android.os.SystemClock; -import android.os.Bundle; -import android.view.View; -import android.view.View.OnClickListener; -import android.widget.Button; -import android.widget.Toast; - -import java.util.Calendar; - -/** - * Example of scheduling one-shot and repeating alarms. See - * {@link OneShotAlarm} for the code run when the one-shot alarm goes off, and - * {@link RepeatingAlarm} for the code run when the repeating alarm goes off. - *

Demo

-App/Service/Alarm Controller - -

Source files

- - - - - - - - - - - - - - - - - -
src/com.example.android.apis/app/AlarmController.javaThe activity that lets you schedule alarms
src/com.example.android.apis/app/OneShotAlarm.javaThis is an intent receiver that executes when the - one-shot alarm goes off
src/com.example.android.apis/app/RepeatingAlarm.javaThis is an intent receiver that executes when the - repeating alarm goes off
/res/any/layout/alarm_controller.xmlDefines contents of the screen
- - */ -public class AlarmController extends Activity { - Toast mToast; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.alarm_controller); - - // Watch for button clicks. - Button button = (Button)findViewById(R.id.one_shot); - button.setOnClickListener(mOneShotListener); - button = (Button)findViewById(R.id.start_repeating); - button.setOnClickListener(mStartRepeatingListener); - button = (Button)findViewById(R.id.stop_repeating); - button.setOnClickListener(mStopRepeatingListener); - } - - private OnClickListener mOneShotListener = new OnClickListener() { - public void onClick(View v) { - // When the alarm goes off, we want to broadcast an Intent to our - // BroadcastReceiver. Here we make an Intent with an explicit class - // name to have our own receiver (which has been published in - // AndroidManifest.xml) instantiated and called, and then create an - // IntentSender to have the intent executed as a broadcast. - Intent intent = new Intent(AlarmController.this, OneShotAlarm.class); - PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, - 0, intent, 0); - - // We want the alarm to go off 30 seconds from now. - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(System.currentTimeMillis()); - calendar.add(Calendar.SECOND, 30); - - // Schedule the alarm! - AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); - am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); - - // Tell the user about what we did. - if (mToast != null) { - mToast.cancel(); - } - mToast = Toast.makeText(AlarmController.this, R.string.one_shot_scheduled, - Toast.LENGTH_LONG); - mToast.show(); - } - }; - - private OnClickListener mStartRepeatingListener = new OnClickListener() { - public void onClick(View v) { - // When the alarm goes off, we want to broadcast an Intent to our - // BroadcastReceiver. Here we make an Intent with an explicit class - // name to have our own receiver (which has been published in - // AndroidManifest.xml) instantiated and called, and then create an - // IntentSender to have the intent executed as a broadcast. - // Note that unlike above, this IntentSender is configured to - // allow itself to be sent multiple times. - Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class); - PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, - 0, intent, 0); - - // We want the alarm to go off 30 seconds from now. - long firstTime = SystemClock.elapsedRealtime(); - firstTime += 15*1000; - - // Schedule the alarm! - AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); - am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, - firstTime, 15*1000, sender); - - // Tell the user about what we did. - if (mToast != null) { - mToast.cancel(); - } - mToast = Toast.makeText(AlarmController.this, R.string.repeating_scheduled, - Toast.LENGTH_LONG); - mToast.show(); - } - }; - - private OnClickListener mStopRepeatingListener = new OnClickListener() { - public void onClick(View v) { - // Create the same intent, and thus a matching IntentSender, for - // the one that was scheduled. - Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class); - PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this, - 0, intent, 0); - - // And cancel the alarm. - AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); - am.cancel(sender); - - // Tell the user about what we did. - if (mToast != null) { - mToast.cancel(); - } - mToast = Toast.makeText(AlarmController.this, R.string.repeating_unscheduled, - Toast.LENGTH_LONG); - mToast.show(); - } - }; -} - diff --git a/mobile/src/main/java/com/example/android/apis/app/AlarmController.kt b/mobile/src/main/java/com/example/android/apis/app/AlarmController.kt new file mode 100644 index 000000000..4ad05a04b --- /dev/null +++ b/mobile/src/main/java/com/example/android/apis/app/AlarmController.kt @@ -0,0 +1,251 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * 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.example.android.apis.app + +// Need the following import to get access to the app resources, since this +// class is in a sub-package. +import com.example.android.apis.R + +import android.app.Activity +import android.app.AlarmManager +import android.app.PendingIntent +import android.content.Intent +import android.os.SystemClock +import android.os.Bundle +import android.view.View +import android.view.View.OnClickListener +import android.widget.Button +import android.widget.Toast + +import java.util.Calendar + +/** + * Example of scheduling one-shot and repeating alarms. See + * [OneShotAlarm] for the code run when the one-shot alarm goes off, and + * [RepeatingAlarm] for the code run when the repeating alarm goes off. + *

Demo

+ * App/Service/Alarm Controller + + *

Source files

+ * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * +
src/com.example.android.apis/app/AlarmController.javaThe activity that lets you schedule alarms
src/com.example.android.apis/app/OneShotAlarm.javaThis is an intent receiver that executes when the + * one-shot alarm goes off
src/com.example.android.apis/app/RepeatingAlarm.javaThis is an intent receiver that executes when the + * repeating alarm goes off
/res/any/layout/alarm_controller.xmlDefines contents of the screen
* + + */ + +class AlarmController : Activity (){ + var mToast: Toast? = null + override fun onCreate(savedInstanceState:Bundle?) : Unit { + super.onCreate(savedInstanceState) + setContentView(R.layout.alarm_controller) + + // Watch for button clicks. + var button = findViewById(R.id.one_shot) as (Button) + button.setOnClickListener(mOneShotListener) + button = findViewById(R.id.start_repeating) as (Button) + button.setOnClickListener(mStartRepeatingListener); + button = findViewById(R.id.stop_repeating) as (Button) + button.setOnClickListener(mStopRepeatingListener); + } + val mOneShotListener = OnClickListener{ + // When the alarm goes off, we want to broadcast an Intent to our + // BroadcastReceiver. Here we make an Intent with an explicit class + // name to have our own receiver (which has been published in + // AndroidManifest.xml) instantiated and called, and then create an + // IntentSender to have the intent executed as a broadcast. + val intent:Intent = Intent(this@AlarmController, OneShotAlarm::class.java) + val sender:PendingIntent = PendingIntent.getBroadcast(this@AlarmController, 0, intent, 0) + + // We want the alarm to go off 30 seconds from now. + var calendar:Calendar = Calendar.getInstance(); + calendar.setTimeInMillis(System.currentTimeMillis()) + calendar.add(Calendar.SECOND, 30); + + // Schedule the alarm! + val am:AlarmManager = getSystemService(ALARM_SERVICE) as (AlarmManager) + am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender) + + // Tell the user about what we did. + if (mToast != null) { + mToast!!.cancel() + } + mToast = Toast.makeText(this@AlarmController, R.string.one_shot_scheduled,Toast.LENGTH_LONG) + mToast!!.show() + + }; + val mStartRepeatingListener = OnClickListener{ + // When the alarm goes off, we want to broadcast an Intent to our + // BroadcastReceiver. Here we make an Intent with an explicit class + // name to have our own receiver (which has been published in + // AndroidManifest.xml) instantiated and called, and then create an + // IntentSender to have the intent executed as a broadcast. + // Note that unlike above, this IntentSender is configured to + // allow itself to be sent multiple times. + val intent:Intent = Intent(this@AlarmController, RepeatingAlarm::class.java) + val sender:PendingIntent = PendingIntent.getBroadcast(this@AlarmController,0, intent, 0) + + // We want the alarm to go off 30 seconds from now. + var firstTime : Long = SystemClock.elapsedRealtime() + firstTime += 15*1000L + + // Schedule the alarm! + val am:AlarmManager = getSystemService(ALARM_SERVICE) as AlarmManager + am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, 15*1000, sender) + + // Tell the user about what we did. + if (mToast != null) { + mToast!!.cancel() + } + mToast = Toast.makeText(this@AlarmController, R.string.repeating_scheduled, Toast.LENGTH_LONG) + mToast!!.show() + + }; + val mStopRepeatingListener = OnClickListener { + // Create the same intent, and thus a matching IntentSender, for + // the one that was scheduled. + val intent:Intent = Intent(this@AlarmController, RepeatingAlarm::class.java) + val sender:PendingIntent = PendingIntent.getBroadcast(this@AlarmController, 0, intent, 0) + + // And cancel the alarm. + val am :AlarmManager= getSystemService(ALARM_SERVICE) as (AlarmManager) + am.cancel(sender) + + // Tell the user about what we did. + if (mToast != null) { + mToast!!.cancel(); + } + mToast = Toast.makeText(this@AlarmController, R.string.repeating_unscheduled,Toast.LENGTH_LONG) + mToast!!.show() + + }; +} +/* + +class AlarmController : Activity() { + internal var mToast: Toast? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + setContentView(R.layout.alarm_controller) + + // Watch for button clicks. + var button = findViewById(R.id.one_shot) as Button + button.setOnClickListener(mOneShotListener) + button = findViewById(R.id.start_repeating) as Button + button.setOnClickListener(mStartRepeatingListener) + button = findViewById(R.id.stop_repeating) as Button + button.setOnClickListener(mStopRepeatingListener) + } + + private val mOneShotListener = OnClickListener { + // When the alarm goes off, we want to broadcast an Intent to our + // BroadcastReceiver. Here we make an Intent with an explicit class + // name to have our own receiver (which has been published in + // AndroidManifest.xml) instantiated and called, and then create an + // IntentSender to have the intent executed as a broadcast. + val intent = Intent(this@AlarmController, OneShotAlarm::class.java) + val sender = PendingIntent.getBroadcast(this@AlarmController, + 0, intent, 0) + + // We want the alarm to go off 30 seconds from now. + val calendar = Calendar.getInstance() + calendar.timeInMillis = System.currentTimeMillis() + calendar.add(Calendar.SECOND, 30) + + // Schedule the alarm! + val am = getSystemService(ALARM_SERVICE) as (AlarmManager) + am.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, sender) + + // Tell the user about what we did. + if (mToast != null) { + mToast!!.cancel() + } + mToast = Toast.makeText(this@AlarmController, R.string.one_shot_scheduled, + Toast.LENGTH_LONG) + mToast!!.show() + } + + private val mStartRepeatingListener = OnClickListener { + // When the alarm goes off, we want to broadcast an Intent to our + // BroadcastReceiver. Here we make an Intent with an explicit class + // name to have our own receiver (which has been published in + // AndroidManifest.xml) instantiated and called, and then create an + // IntentSender to have the intent executed as a broadcast. + // Note that unlike above, this IntentSender is configured to + // allow itself to be sent multiple times. + val intent = Intent(this@AlarmController, RepeatingAlarm::class.java) + val sender = PendingIntent.getBroadcast(this@AlarmController, + 0, intent, 0) + + // We want the alarm to go off 30 seconds from now. + var firstTime = SystemClock.elapsedRealtime() + firstTime += (15 * 1000).toLong() + + // Schedule the alarm! + val am = getSystemService(ALARM_SERVICE) as (AlarmManager) + am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, + firstTime, (15 * 1000).toLong(), sender) + + // Tell the user about what we did. + if (mToast != null) { + mToast!!.cancel() + } + mToast = Toast.makeText(this@AlarmController, R.string.repeating_scheduled, + Toast.LENGTH_LONG) + mToast!!.show() + } + + private val mStopRepeatingListener = OnClickListener { + // Create the same intent, and thus a matching IntentSender, for + // the one that was scheduled. + val intent = Intent(this@AlarmController, RepeatingAlarm::class.java) + val sender = PendingIntent.getBroadcast(this@AlarmController, + 0, intent, 0) + + // And cancel the alarm. + val am = getSystemService(ALARM_SERVICE) as (AlarmManager) + am.cancel(sender) + + // Tell the user about what we did. + if (mToast != null) { + mToast!!.cancel() + } + mToast = Toast.makeText(this@AlarmController, R.string.repeating_unscheduled, + Toast.LENGTH_LONG) + mToast!!.show() + } +} +*/ + From 4baef90ee880b63a199b722b41ef12c2faccc072 Mon Sep 17 00:00:00 2001 From: lyn Date: Fri, 11 Aug 2017 21:58:41 +0900 Subject: [PATCH 08/34] removed comment --- .../android/apis/app/AlarmController.kt | 99 ------------------- 1 file changed, 99 deletions(-) diff --git a/mobile/src/main/java/com/example/android/apis/app/AlarmController.kt b/mobile/src/main/java/com/example/android/apis/app/AlarmController.kt index 4ad05a04b..37aff5d75 100644 --- a/mobile/src/main/java/com/example/android/apis/app/AlarmController.kt +++ b/mobile/src/main/java/com/example/android/apis/app/AlarmController.kt @@ -61,7 +61,6 @@ import java.util.Calendar * Defines contents of the screen * * - */ class AlarmController : Activity (){ @@ -150,102 +149,4 @@ class AlarmController : Activity (){ }; } -/* - -class AlarmController : Activity() { - internal var mToast: Toast? = null - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - setContentView(R.layout.alarm_controller) - - // Watch for button clicks. - var button = findViewById(R.id.one_shot) as Button - button.setOnClickListener(mOneShotListener) - button = findViewById(R.id.start_repeating) as Button - button.setOnClickListener(mStartRepeatingListener) - button = findViewById(R.id.stop_repeating) as Button - button.setOnClickListener(mStopRepeatingListener) - } - - private val mOneShotListener = OnClickListener { - // When the alarm goes off, we want to broadcast an Intent to our - // BroadcastReceiver. Here we make an Intent with an explicit class - // name to have our own receiver (which has been published in - // AndroidManifest.xml) instantiated and called, and then create an - // IntentSender to have the intent executed as a broadcast. - val intent = Intent(this@AlarmController, OneShotAlarm::class.java) - val sender = PendingIntent.getBroadcast(this@AlarmController, - 0, intent, 0) - - // We want the alarm to go off 30 seconds from now. - val calendar = Calendar.getInstance() - calendar.timeInMillis = System.currentTimeMillis() - calendar.add(Calendar.SECOND, 30) - - // Schedule the alarm! - val am = getSystemService(ALARM_SERVICE) as (AlarmManager) - am.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, sender) - - // Tell the user about what we did. - if (mToast != null) { - mToast!!.cancel() - } - mToast = Toast.makeText(this@AlarmController, R.string.one_shot_scheduled, - Toast.LENGTH_LONG) - mToast!!.show() - } - - private val mStartRepeatingListener = OnClickListener { - // When the alarm goes off, we want to broadcast an Intent to our - // BroadcastReceiver. Here we make an Intent with an explicit class - // name to have our own receiver (which has been published in - // AndroidManifest.xml) instantiated and called, and then create an - // IntentSender to have the intent executed as a broadcast. - // Note that unlike above, this IntentSender is configured to - // allow itself to be sent multiple times. - val intent = Intent(this@AlarmController, RepeatingAlarm::class.java) - val sender = PendingIntent.getBroadcast(this@AlarmController, - 0, intent, 0) - - // We want the alarm to go off 30 seconds from now. - var firstTime = SystemClock.elapsedRealtime() - firstTime += (15 * 1000).toLong() - - // Schedule the alarm! - val am = getSystemService(ALARM_SERVICE) as (AlarmManager) - am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, - firstTime, (15 * 1000).toLong(), sender) - - // Tell the user about what we did. - if (mToast != null) { - mToast!!.cancel() - } - mToast = Toast.makeText(this@AlarmController, R.string.repeating_scheduled, - Toast.LENGTH_LONG) - mToast!!.show() - } - - private val mStopRepeatingListener = OnClickListener { - // Create the same intent, and thus a matching IntentSender, for - // the one that was scheduled. - val intent = Intent(this@AlarmController, RepeatingAlarm::class.java) - val sender = PendingIntent.getBroadcast(this@AlarmController, - 0, intent, 0) - - // And cancel the alarm. - val am = getSystemService(ALARM_SERVICE) as (AlarmManager) - am.cancel(sender) - - // Tell the user about what we did. - if (mToast != null) { - mToast!!.cancel() - } - mToast = Toast.makeText(this@AlarmController, R.string.repeating_unscheduled, - Toast.LENGTH_LONG) - mToast!!.show() - } -} -*/ From 01b8e3654dce7c1a050ae9bfa838855f9037f22d Mon Sep 17 00:00:00 2001 From: lyn Date: Fri, 11 Aug 2017 21:59:21 +0900 Subject: [PATCH 09/34] used by lazy for t2(textView) --- mobile/src/main/java/com/example/android/apis/text/Link.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mobile/src/main/java/com/example/android/apis/text/Link.kt b/mobile/src/main/java/com/example/android/apis/text/Link.kt index b7ae154d1..6a1276fc7 100644 --- a/mobile/src/main/java/com/example/android/apis/text/Link.kt +++ b/mobile/src/main/java/com/example/android/apis/text/Link.kt @@ -46,7 +46,9 @@ class Link : Activity() { // respond to user input. To make them active, you need to // call setMovementMethod() on the TextView object. - val t2 = findViewById(R.id.text2) as TextView + val t2 :TextView by lazy { + findViewById(R.id.text2) as TextView + } t2.movementMethod = LinkMovementMethod.getInstance() // text3 shows creating text with links from HTML in the Java From b1d8c181322e25bd96834132484be80002540a09 Mon Sep 17 00:00:00 2001 From: lyn Date: Fri, 11 Aug 2017 22:11:00 +0900 Subject: [PATCH 10/34] used lazy for t3 (textView) --- .../com/example/android/apis/text/Link.kt | 57 ++++++++++--------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/mobile/src/main/java/com/example/android/apis/text/Link.kt b/mobile/src/main/java/com/example/android/apis/text/Link.kt index 6a1276fc7..e0bacc39f 100644 --- a/mobile/src/main/java/com/example/android/apis/text/Link.kt +++ b/mobile/src/main/java/com/example/android/apis/text/Link.kt @@ -46,7 +46,7 @@ class Link : Activity() { // respond to user input. To make them active, you need to // call setMovementMethod() on the TextView object. - val t2 :TextView by lazy { + val t2 by lazy { findViewById(R.id.text2) as TextView } t2.movementMethod = LinkMovementMethod.getInstance() @@ -58,36 +58,39 @@ class Link : Activity() { // illustrate how you might display text that came from a // dynamic source (eg, the network). - val t3 = findViewById(R.id.text3) as TextView - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { - t3.text = Html.fromHtml( - "text3: Constructed from HTML programmatically. Text with a " + - "link " + - "created in the Java source code using HTML.", Html.FROM_HTML_MODE_LEGACY) + val t3 by lazt { + findViewById(R.id.text3) as TextView } - else{ - t3.text = Html.fromHtml( - "text3: Constructed from HTML programmatically. Text with a " + - "link " + - "created in the Java source code using HTML.") - } - t3.movementMethod = LinkMovementMethod.getInstance() + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + t3.text = Html.fromHtml( + "text3: Constructed from HTML programmatically. Text with a " + + "link " + + "created in the Java source code using HTML.", Html.FROM_HTML_MODE_LEGACY) + } + else{ + t3.text = Html.fromHtml( + "text3: Constructed from HTML programmatically. Text with a " + + "link " + + "created in the Java source code using HTML.") + } + t3.movementMethod = LinkMovementMethod.getInstance() - // text4 illustrates constructing a styled string containing a - // link without using HTML at all. Again, for a fixed string - // you should probably be using a string resource, not a - // hardcoded value. + // text4 illustrates constructing a styled string containing a + // link without using HTML at all. Again, for a fixed string + // you should probably be using a string resource, not a + // hardcoded value. - val ss = SpannableString( - "text4: Manually created spans. Click here to dial the phone.") + val ss = SpannableString( + "text4: Manually created spans. Click here to dial the phone.") - ss.setSpan(StyleSpan(Typeface.BOLD), 0, 30, - Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) - ss.setSpan(URLSpan("tel:4155551212"), 31 + 6, 31 + 10, - Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + ss.setSpan(StyleSpan(Typeface.BOLD), 0, 30, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + ss.setSpan(URLSpan("tel:4155551212"), 31 + 6, 31 + 10, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) - val t4 = findViewById(R.id.text4) as TextView - t4.text = ss - t4.movementMethod = LinkMovementMethod.getInstance() + val t4 = findViewById(R.id.text4) as TextView + t4.text = ss + t4.movementMethod = LinkMovementMethod.getInstance() + } } } From fed790ff5bc946a2737d36980adff92fdd07dac3 Mon Sep 17 00:00:00 2001 From: lyn Date: Fri, 11 Aug 2017 22:25:36 +0900 Subject: [PATCH 11/34] used lazy for val t4 --- .../com/example/android/apis/text/Link.kt | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/mobile/src/main/java/com/example/android/apis/text/Link.kt b/mobile/src/main/java/com/example/android/apis/text/Link.kt index e0bacc39f..ad6b2e3ef 100644 --- a/mobile/src/main/java/com/example/android/apis/text/Link.kt +++ b/mobile/src/main/java/com/example/android/apis/text/Link.kt @@ -58,39 +58,41 @@ class Link : Activity() { // illustrate how you might display text that came from a // dynamic source (eg, the network). - val t3 by lazt { + val t3 by lazy { findViewById(R.id.text3) as TextView } - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { - t3.text = Html.fromHtml( - "text3: Constructed from HTML programmatically. Text with a " + - "link " + - "created in the Java source code using HTML.", Html.FROM_HTML_MODE_LEGACY) - } - else{ - t3.text = Html.fromHtml( - "text3: Constructed from HTML programmatically. Text with a " + - "link " + - "created in the Java source code using HTML.") - } - t3.movementMethod = LinkMovementMethod.getInstance() + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + t3.text = Html.fromHtml( + "text3: Constructed from HTML programmatically. Text with a " + + "link " + + "created in the Java source code using HTML.", Html.FROM_HTML_MODE_LEGACY) + } + else{ + t3.text = Html.fromHtml( + "text3: Constructed from HTML programmatically. Text with a " + + "link " + + "created in the Java source code using HTML.") + } + t3.movementMethod = LinkMovementMethod.getInstance() - // text4 illustrates constructing a styled string containing a - // link without using HTML at all. Again, for a fixed string - // you should probably be using a string resource, not a - // hardcoded value. + // text4 illustrates constructing a styled string containing a + // link without using HTML at all. Again, for a fixed string + // you should probably be using a string resource, not a + // hardcoded value. - val ss = SpannableString( - "text4: Manually created spans. Click here to dial the phone.") + val ss = SpannableString( + "text4: Manually created spans. Click here to dial the phone.") - ss.setSpan(StyleSpan(Typeface.BOLD), 0, 30, - Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) - ss.setSpan(URLSpan("tel:4155551212"), 31 + 6, 31 + 10, - Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + ss.setSpan(StyleSpan(Typeface.BOLD), 0, 30, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + ss.setSpan(URLSpan("tel:4155551212"), 31 + 6, 31 + 10, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) - val t4 = findViewById(R.id.text4) as TextView - t4.text = ss - t4.movementMethod = LinkMovementMethod.getInstance() + val t4 by lazy{ + findViewById(R.id.text4) as TextView } + t4.text = ss + t4.movementMethod = LinkMovementMethod.getInstance() } } + From 88ec3aeec53b4cb902039450f9ae519a5f2f7623 Mon Sep 17 00:00:00 2001 From: lyn Date: Fri, 11 Aug 2017 22:27:42 +0900 Subject: [PATCH 12/34] used lazy to init var mText and removed !!.apend --- .../main/java/com/example/android/apis/text/LogTextBox1.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.kt b/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.kt index b482bec17..d0c332c26 100644 --- a/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.kt +++ b/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.kt @@ -30,16 +30,17 @@ import android.widget.Button; */ class LogTextBox1 : Activity() { - private var mText: LogTextBox? = null + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.log_text_box_1) - mText = findViewById(R.id.text) as LogTextBox + val mText by lazy { + findViewById(R.id.text) as LogTextBox } val addButton = findViewById(R.id.add) as Button - addButton.setOnClickListener { mText!!.append("This is a test\n") } + addButton.setOnClickListener { mText.append("This is a test\n") } } } From 37930846bfe72eb75edcce6d18cee2ec1a078ff0 Mon Sep 17 00:00:00 2001 From: lyn Date: Fri, 11 Aug 2017 22:42:05 +0900 Subject: [PATCH 13/34] use Kotlin synthetic property instead of java calendar.set() --- .../example/android/apis/app/AlarmController.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mobile/src/main/java/com/example/android/apis/app/AlarmController.kt b/mobile/src/main/java/com/example/android/apis/app/AlarmController.kt index 37aff5d75..ad81fecfc 100644 --- a/mobile/src/main/java/com/example/android/apis/app/AlarmController.kt +++ b/mobile/src/main/java/com/example/android/apis/app/AlarmController.kt @@ -70,12 +70,12 @@ class AlarmController : Activity (){ setContentView(R.layout.alarm_controller) // Watch for button clicks. - var button = findViewById(R.id.one_shot) as (Button) + var button = findViewById(R.id.one_shot) button.setOnClickListener(mOneShotListener) - button = findViewById(R.id.start_repeating) as (Button) - button.setOnClickListener(mStartRepeatingListener); - button = findViewById(R.id.stop_repeating) as (Button) - button.setOnClickListener(mStopRepeatingListener); + button = findViewById(R.id.start_repeating) + button.setOnClickListener(mStartRepeatingListener) + button = findViewById(R.id.stop_repeating) + button.setOnClickListener(mStopRepeatingListener) } val mOneShotListener = OnClickListener{ // When the alarm goes off, we want to broadcast an Intent to our @@ -87,9 +87,9 @@ class AlarmController : Activity (){ val sender:PendingIntent = PendingIntent.getBroadcast(this@AlarmController, 0, intent, 0) // We want the alarm to go off 30 seconds from now. - var calendar:Calendar = Calendar.getInstance(); - calendar.setTimeInMillis(System.currentTimeMillis()) - calendar.add(Calendar.SECOND, 30); + var calendar:Calendar = Calendar.getInstance() + calendar.timeInMillis=System.currentTimeMillis() + calendar.add(Calendar.SECOND, 30) // Schedule the alarm! val am:AlarmManager = getSystemService(ALARM_SERVICE) as (AlarmManager) From f96574809bfd9831079ccf97ad9cfa784b518eba Mon Sep 17 00:00:00 2001 From: lyn Date: Sat, 12 Aug 2017 03:05:23 +0900 Subject: [PATCH 14/34] apply plugin: 'kotlin-android-extensions' --- mobile/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mobile/build.gradle b/mobile/build.gradle index 0f3641797..d9279e47e 100644 --- a/mobile/build.gradle +++ b/mobile/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' - +apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 buildToolsVersion "26.0.0" From b3a28ad5c03430809b4bb9c3b3b1bdda80bb03b4 Mon Sep 17 00:00:00 2001 From: lyn Date: Sat, 12 Aug 2017 03:09:36 +0900 Subject: [PATCH 15/34] used extention code instead of findViewById --- .../main/java/com/example/android/apis/text/LogTextBox1.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.kt b/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.kt index d0c332c26..75e5010e8 100644 --- a/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.kt +++ b/mobile/src/main/java/com/example/android/apis/text/LogTextBox1.kt @@ -21,7 +21,7 @@ import com.example.android.apis.R; import android.app.Activity; import android.os.Bundle; import android.view.View; -import android.widget.Button; +import kotlinx.android.synthetic.main.log_text_box_1.* /** * Using a LogTextBox to display a scrollable text area @@ -39,8 +39,7 @@ class LogTextBox1 : Activity() { val mText by lazy { findViewById(R.id.text) as LogTextBox } - val addButton = findViewById(R.id.add) as Button - addButton.setOnClickListener { mText.append("This is a test\n") } + add.setOnClickListener { mText.append("This is a test\n") } } } From 1ae905ec95cd48b8c08c96972e72bdb7de4de5aa Mon Sep 17 00:00:00 2001 From: lyn Date: Sat, 12 Aug 2017 03:41:36 +0900 Subject: [PATCH 16/34] convert AlarmService to kt --- .../android/apis/app/AlarmService.java | 89 ------------------- .../example/android/apis/app/AlarmService.kt | 77 ++++++++++++++++ 2 files changed, 77 insertions(+), 89 deletions(-) delete mode 100644 mobile/src/main/java/com/example/android/apis/app/AlarmService.java create mode 100644 mobile/src/main/java/com/example/android/apis/app/AlarmService.kt diff --git a/mobile/src/main/java/com/example/android/apis/app/AlarmService.java b/mobile/src/main/java/com/example/android/apis/app/AlarmService.java deleted file mode 100644 index 151838ae2..000000000 --- a/mobile/src/main/java/com/example/android/apis/app/AlarmService.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * 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.example.android.apis.app; - -// Need the following import to get access to the app resources, since this -// class is in a sub-package. -import com.example.android.apis.R; - -import android.app.Activity; -import android.app.AlarmManager; -import android.app.PendingIntent; -import android.content.Intent; -import android.os.SystemClock; -import android.os.Bundle; -import android.view.View; -import android.view.View.OnClickListener; -import android.widget.Button; -import android.widget.Toast; - - -/** - * This demonstrates how you can schedule an alarm that causes a service to - * be started. This is useful when you want to schedule alarms that initiate - * long-running operations, such as retrieving recent e-mails. - */ -public class AlarmService extends Activity { - private PendingIntent mAlarmSender; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Create an IntentSender that will launch our service, to be scheduled - // with the alarm manager. - mAlarmSender = PendingIntent.getService(AlarmService.this, - 0, new Intent(AlarmService.this, AlarmService_Service.class), 0); - - setContentView(R.layout.alarm_service); - - // Watch for button clicks. - Button button = (Button)findViewById(R.id.start_alarm); - button.setOnClickListener(mStartAlarmListener); - button = (Button)findViewById(R.id.stop_alarm); - button.setOnClickListener(mStopAlarmListener); - } - - private OnClickListener mStartAlarmListener = new OnClickListener() { - public void onClick(View v) { - // We want the alarm to go off 30 seconds from now. - long firstTime = SystemClock.elapsedRealtime(); - - // Schedule the alarm! - AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); - am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, - firstTime, 30*1000, mAlarmSender); - - // Tell the user about what we did. - Toast.makeText(AlarmService.this, R.string.repeating_scheduled, - Toast.LENGTH_LONG).show(); - } - }; - - private OnClickListener mStopAlarmListener = new OnClickListener() { - public void onClick(View v) { - // And cancel the alarm. - AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); - am.cancel(mAlarmSender); - - // Tell the user about what we did. - Toast.makeText(AlarmService.this, R.string.repeating_unscheduled, - Toast.LENGTH_LONG).show(); - - } - }; -} diff --git a/mobile/src/main/java/com/example/android/apis/app/AlarmService.kt b/mobile/src/main/java/com/example/android/apis/app/AlarmService.kt new file mode 100644 index 000000000..15b7e617f --- /dev/null +++ b/mobile/src/main/java/com/example/android/apis/app/AlarmService.kt @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * 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.example.android.apis.app; + +// Need the following import to get access to the app resources, since this +// class is in a sub-package. +import com.example.android.apis.R; + +import android.app.Activity; +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.content.Intent; +import android.os.SystemClock; +import android.os.Bundle; +import android.view.View.OnClickListener; +import android.widget.Toast; +import kotlinx.android.synthetic.main.alarm_service.* + + +class AlarmService : Activity() { + lateinit var mAlarmSender : PendingIntent + + override fun onCreate( savedInstanceState : Bundle?) { + super.onCreate(savedInstanceState) + + // Create an IntentSender that will launch our service, to be scheduled + // with the alarm manager. + mAlarmSender = PendingIntent.getService(this@AlarmService, + 0, Intent(this@AlarmService, AlarmService_Service::class.java), 0) + + setContentView(R.layout.alarm_service) + + // Watch for button clicks. + start_alarm.setOnClickListener(mStartAlarmListener) + stop_alarm.setOnClickListener(mStopAlarmListener) + } + + private val mStartAlarmListener= OnClickListener { + // We want the alarm to go off 30 seconds from now. + var firstTime : Long = SystemClock.elapsedRealtime() + + // Schedule the alarm! + var am :AlarmManager = getSystemService(ALARM_SERVICE) as AlarmManager + am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, + firstTime, 30*1000, mAlarmSender) + + // Tell the user about what we did. + Toast.makeText(this@AlarmService, R.string.repeating_scheduled, + Toast.LENGTH_LONG).show() + } + + private val mStopAlarmListener = OnClickListener{ + // And cancel the alarm. + var am :AlarmManager = getSystemService(ALARM_SERVICE) as AlarmManager + am.cancel(mAlarmSender) + + // Tell the user about what we did. + Toast.makeText(this@AlarmService, R.string.repeating_unscheduled, + Toast.LENGTH_LONG).show() + + + } +} \ No newline at end of file From aa830bf2c13024832c6472c219122009d3d9f820 Mon Sep 17 00:00:00 2001 From: lyn Date: Sat, 12 Aug 2017 19:02:09 +0900 Subject: [PATCH 17/34] FragmentAlertDialog converted to kt --- ...lertDialog.java => FragmentAlertDialog.kt} | 77 ++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) rename mobile/src/main/java/com/example/android/apis/app/{FragmentAlertDialog.java => FragmentAlertDialog.kt} (58%) diff --git a/mobile/src/main/java/com/example/android/apis/app/FragmentAlertDialog.java b/mobile/src/main/java/com/example/android/apis/app/FragmentAlertDialog.kt similarity index 58% rename from mobile/src/main/java/com/example/android/apis/app/FragmentAlertDialog.java rename to mobile/src/main/java/com/example/android/apis/app/FragmentAlertDialog.kt index 56ddc6b91..9efab7207 100644 --- a/mobile/src/main/java/com/example/android/apis/app/FragmentAlertDialog.java +++ b/mobile/src/main/java/com/example/android/apis/app/FragmentAlertDialog.kt @@ -33,7 +33,7 @@ /** * Demonstrates how to show an AlertDialog that is managed by a Fragment. */ -public class FragmentAlertDialog extends Activity { +/*public class FragmentAlertDialog extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { @@ -106,4 +106,79 @@ public void onClick(DialogInterface dialog, int whichButton) { } } //END_INCLUDE(dialog) +}*/ +class FragmentAlertDialog : Activity() { + + override protected fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.fragment_dialog) + + val tv = findViewById(R.id.text) + tv.setText("Example of displaying an alert dialog with a DialogFragment") + + // Watch for button clicks. + val button = findViewById