wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package nodomain.freeyourgadget.gadgetbridge.devices.idasen;
 
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.text.DecimalFormat;
import java.util.Objects;
 
 
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.workouts.WorkoutValueFormatter;
import nodomain.freeyourgadget.gadgetbridge.service.devices.idasen.IdasenDeviceSupport;
 
public class IdasenControlActivity extends AbstractGBActivity {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    public TextView mDeskHeight, mDeskSpeed;
 
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Objects.requireNonNull(action).equals(IdasenConstants.ACTION_REALTIME_DESK_VALUES)){
                final WorkoutValueFormatter formatter = new WorkoutValueFormatter();
                float height = (float) intent.getSerializableExtra(IdasenConstants.EXTRA_DESK_HEIGHT);
                float speed = (float) intent.getSerializableExtra(IdasenConstants.EXTRA_DESK_SPEED);
                logger.debug("Received desk values: {} {}", speed, height);
                mDeskHeight.setText(formatter.formatValue(height * 100F, "cm"));
                mDeskSpeed.setText(formatter.formatValue(speed * 1000F, "mm/s"));
            }
        }
    };
 
    LocalBroadcastManager localBroadcastManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_idasen_control);
 
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
 
        mDeskHeight = findViewById(R.id.idasen_desk_height);
        mDeskSpeed = findViewById(R.id.idasen_desk_speed);
 
        IntentFilter filterLocal = new IntentFilter();
        filterLocal.addAction(IdasenConstants.ACTION_REALTIME_DESK_VALUES);
        LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(mReceiver, filterLocal);
        Intent intent = new Intent(IdasenDeviceSupport.COMMAND_GET_DESK_VALUES);
        sendLocalBroadcast(intent);
 
        View.OnTouchListener controlTouchListener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Intent intent = null;
 
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (v.getId() == R.id.idasen_control_button_up) {
                         intent = new Intent(IdasenDeviceSupport.COMMAND_UP);
                    } else if (v.getId() == R.id.idasen_control_button_down) {
                         intent = new Intent(IdasenDeviceSupport.COMMAND_DOWN);
                    } else if (v.getId() == R.id.idasen_control_button_sit) {
                         intent = new Intent(IdasenDeviceSupport.COMMAND_SET_HEIGHT);
                        intent.putExtra(IdasenDeviceSupport.TARGET_HEIGHT, IdasenDeviceSupport.TARGET_POS_SIT);
                    } else if (v.getId() == R.id.idasen_control_button_stand) {
                         intent = new Intent(IdasenDeviceSupport.COMMAND_SET_HEIGHT);
                        intent.putExtra(IdasenDeviceSupport.TARGET_HEIGHT, IdasenDeviceSupport.TARGET_POS_STAND);
                    } else if (v.getId() == R.id.idasen_control_button_mid) {
                         intent = new Intent(IdasenDeviceSupport.COMMAND_SET_HEIGHT);
                        intent.putExtra(IdasenDeviceSupport.TARGET_HEIGHT, IdasenDeviceSupport.TARGET_POS_MID);
                    }
                    sendLocalBroadcast(intent);
                } else {
                    return false;
                }
                return true;
            }
        };
        findViewById(R.id.idasen_control_button_up).setOnTouchListener(controlTouchListener);
        findViewById(R.id.idasen_control_button_down).setOnTouchListener(controlTouchListener);
        findViewById(R.id.idasen_control_button_sit).setOnTouchListener(controlTouchListener);
        findViewById(R.id.idasen_control_button_mid).setOnTouchListener(controlTouchListener);
        findViewById(R.id.idasen_control_button_stand).setOnTouchListener(controlTouchListener);
    }
 
    private void sendLocalBroadcast(Intent intent) {
        localBroadcastManager.sendBroadcast(intent);
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(mReceiver);
    }
}