AcceptActivity -> kt
This commit is contained in:
parent
3b54b24a68
commit
5ca9390f07
2 changed files with 135 additions and 169 deletions
|
@ -1,169 +0,0 @@
|
|||
package info.nightscout.androidaps.interaction.actions;
|
||||
|
||||
import static info.nightscout.androidaps.comm.DataLayerListenerServiceWear.KEY_ACTION_DATA;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.SystemClock;
|
||||
import android.os.Vibrator;
|
||||
import android.support.wearable.view.GridPagerAdapter;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewConfiguration;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.core.view.InputDeviceCompat;
|
||||
import androidx.core.view.MotionEventCompat;
|
||||
import androidx.core.view.ViewConfigurationCompat;
|
||||
|
||||
import info.nightscout.androidaps.R;
|
||||
import info.nightscout.androidaps.events.EventWearToMobile;
|
||||
import info.nightscout.shared.weardata.EventData;
|
||||
|
||||
/**
|
||||
* Created by adrian on 09/02/17.
|
||||
*/
|
||||
|
||||
public class AcceptActivity extends ViewSelectorActivity {
|
||||
|
||||
String message = "";
|
||||
String actionKey = "";
|
||||
private DismissThread dismissThread;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
this.dismissThread = new DismissThread();
|
||||
dismissThread.start();
|
||||
|
||||
Bundle extras = getIntent().getExtras();
|
||||
message = extras.getString("message", "");
|
||||
actionKey = extras.getString(KEY_ACTION_DATA, "");
|
||||
|
||||
if (message.isEmpty() || actionKey.isEmpty()) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
setAdapter(new MyGridViewPagerAdapter());
|
||||
|
||||
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
|
||||
long[] vibratePattern = new long[]{0, 100, 50, 100, 50};
|
||||
v.vibrate(vibratePattern, -1);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
finish();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private class MyGridViewPagerAdapter extends GridPagerAdapter {
|
||||
@Override
|
||||
public int getColumnCount(int arg0) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRowCount() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object instantiateItem(ViewGroup container, int row, int col) {
|
||||
|
||||
final View view;
|
||||
if (col == 0) {
|
||||
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_confirm_text, container, false);
|
||||
final TextView textView = view.findViewById(R.id.message);
|
||||
final View scrollView = view.findViewById(R.id.message_scroll);
|
||||
textView.setText(message);
|
||||
container.addView(view);
|
||||
scrollView.setOnGenericMotionListener((v, ev) -> {
|
||||
if (ev.getAction() == MotionEvent.ACTION_SCROLL &&
|
||||
ev.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)
|
||||
) {
|
||||
float delta = -ev.getAxisValue(MotionEventCompat.AXIS_SCROLL) *
|
||||
ViewConfigurationCompat.getScaledVerticalScrollFactor(
|
||||
ViewConfiguration.get(container.getContext()),
|
||||
container.getContext());
|
||||
v.scrollBy(0, Math.round(delta));
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
scrollView.requestFocus();
|
||||
} else {
|
||||
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.action_send_item, container, false);
|
||||
final ImageView confirmButton = view.findViewById(R.id.confirmbutton);
|
||||
confirmButton.setOnClickListener((View v) -> {
|
||||
EventData returnCommand = EventData.Companion.deserialize(actionKey);
|
||||
rxBus.send(new EventWearToMobile(returnCommand));
|
||||
rxBus.send(new EventData.CancelNotification(System.currentTimeMillis()));
|
||||
finishAffinity();
|
||||
});
|
||||
container.addView(view);
|
||||
}
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroyItem(ViewGroup container, int row, int col, Object view) {
|
||||
// Handle this to get the data before the view is destroyed?
|
||||
// Object should still be kept by this, just setup for re-init?
|
||||
container.removeView((View) view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isViewFromObject(View view, Object object) {
|
||||
return view == object;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (dismissThread != null) {
|
||||
dismissThread.invalidate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class DismissThread extends Thread {
|
||||
private boolean valid = true;
|
||||
|
||||
public synchronized void invalidate() {
|
||||
valid = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
SystemClock.sleep(60 * 1000);
|
||||
synchronized (this) {
|
||||
if (valid) {
|
||||
AcceptActivity.this.finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected synchronized void onNewIntent(Intent intent) {
|
||||
super.onNewIntent(intent);
|
||||
if (dismissThread != null) dismissThread.invalidate();
|
||||
Bundle extras = intent.getExtras();
|
||||
Intent msgIntent = new Intent(this, AcceptActivity.class);
|
||||
msgIntent.putExtras(extras);
|
||||
startActivity(msgIntent);
|
||||
finish();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
@file:Suppress("DEPRECATION")
|
||||
|
||||
package info.nightscout.androidaps.interaction.actions
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.os.SystemClock
|
||||
import android.os.Vibrator
|
||||
import android.support.wearable.view.GridPagerAdapter
|
||||
import android.view.LayoutInflater
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.ViewConfiguration
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.core.view.InputDeviceCompat
|
||||
import androidx.core.view.MotionEventCompat
|
||||
import androidx.core.view.ViewConfigurationCompat
|
||||
import info.nightscout.androidaps.R
|
||||
import info.nightscout.androidaps.comm.DataLayerListenerServiceWear
|
||||
import info.nightscout.androidaps.events.EventWearToMobile
|
||||
import info.nightscout.shared.weardata.EventData.CancelNotification
|
||||
import info.nightscout.shared.weardata.EventData.Companion.deserialize
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
class AcceptActivity : ViewSelectorActivity() {
|
||||
|
||||
var message = ""
|
||||
var actionKey = ""
|
||||
private var dismissThread: DismissThread? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
dismissThread = DismissThread()
|
||||
dismissThread!!.start()
|
||||
val extras = intent.extras
|
||||
message = extras!!.getString("message", "")
|
||||
actionKey = extras.getString(DataLayerListenerServiceWear.KEY_ACTION_DATA, "")
|
||||
if (message.isEmpty() || actionKey.isEmpty()) {
|
||||
finish()
|
||||
return
|
||||
}
|
||||
setAdapter(MyGridViewPagerAdapter())
|
||||
val v = getSystemService(VIBRATOR_SERVICE) as Vibrator
|
||||
val vibratePattern = longArrayOf(0, 100, 50, 100, 50)
|
||||
v.vibrate(vibratePattern, -1)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
finish()
|
||||
}
|
||||
|
||||
private inner class MyGridViewPagerAdapter : GridPagerAdapter() {
|
||||
|
||||
override fun getColumnCount(arg0: Int): Int = 2
|
||||
override fun getRowCount(): Int = 1
|
||||
|
||||
override fun instantiateItem(container: ViewGroup, row: Int, col: Int): Any {
|
||||
val view: View
|
||||
if (col == 0) {
|
||||
view = LayoutInflater.from(applicationContext).inflate(R.layout.action_confirm_text, container, false)
|
||||
val textView = view.findViewById<TextView>(R.id.message)
|
||||
val scrollView = view.findViewById<View>(R.id.message_scroll)
|
||||
textView.text = message
|
||||
container.addView(view)
|
||||
scrollView.setOnGenericMotionListener { v: View, ev: MotionEvent ->
|
||||
if (ev.action == MotionEvent.ACTION_SCROLL &&
|
||||
ev.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER)
|
||||
) {
|
||||
val delta = -ev.getAxisValue(MotionEventCompat.AXIS_SCROLL) *
|
||||
ViewConfigurationCompat.getScaledVerticalScrollFactor(
|
||||
ViewConfiguration.get(container.context),
|
||||
container.context
|
||||
)
|
||||
v.scrollBy(0, delta.roundToInt())
|
||||
return@setOnGenericMotionListener true
|
||||
}
|
||||
false
|
||||
}
|
||||
scrollView.requestFocus()
|
||||
} else {
|
||||
view = LayoutInflater.from(applicationContext).inflate(R.layout.action_send_item, container, false)
|
||||
val confirmButton = view.findViewById<ImageView>(R.id.confirmbutton)
|
||||
confirmButton.setOnClickListener {
|
||||
val returnCommand = deserialize(actionKey)
|
||||
rxBus.send(EventWearToMobile(returnCommand))
|
||||
rxBus.send(CancelNotification(System.currentTimeMillis()))
|
||||
finishAffinity()
|
||||
}
|
||||
container.addView(view)
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
override fun destroyItem(container: ViewGroup, row: Int, col: Int, view: Any) {
|
||||
// Handle this to get the data before the view is destroyed?
|
||||
// Object should still be kept by this, just setup for re-init?
|
||||
container.removeView(view as View)
|
||||
}
|
||||
|
||||
override fun isViewFromObject(view: View, `object`: Any): Boolean = view === `object`
|
||||
}
|
||||
|
||||
@Synchronized public override fun onDestroy() {
|
||||
dismissThread?.invalidate()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private inner class DismissThread : Thread() {
|
||||
|
||||
private var valid = true
|
||||
@Synchronized fun invalidate() {
|
||||
valid = false
|
||||
}
|
||||
|
||||
override fun run() {
|
||||
SystemClock.sleep((60 * 1000L))
|
||||
synchronized(this) {
|
||||
if (valid) finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
if (dismissThread != null) dismissThread!!.invalidate()
|
||||
val extras = intent.extras
|
||||
val msgIntent = Intent(this, AcceptActivity::class.java)
|
||||
msgIntent.putExtras(extras!!)
|
||||
startActivity(msgIntent)
|
||||
finish()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue