MainActivity.java 17.6 KB
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
package com.example.talktalkspeak;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
    private static final String ACTION_NOTIFICATION_LISTENER_SETTINGS = "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS";
    private static final int REQUEST_ENABLE_BT = 10;
    ArrayAdapter<String> arrayAdapter;
    public AudioManager audioManager;
    private BluetoothAdapter bluetoothAdapter;
    BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
            if ("android.bluetooth.device.action.ACL_CONNECTED".equals(action)) {
                MainActivity.this.deviceview.setText(device.getName().toString());
            } else if ("android.bluetooth.device.action.ACL_DISCONNECTED".equals(action)) {
                MainActivity.this.deviceview.setText("없음");
            }
        }
    };

    /* access modifiers changed from: private */
    public Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;

    public TextView deviceview;
    private TextView editText;
    ArrayList<String> engineList;
    ArrayList<String> engineSpinnerList;

    private int isTTSReady = -2;
    private BroadcastReceiver kakaoReceiver;
    long now = System.currentTimeMillis();


    /* access modifiers changed from: private */
    public double speed = 1.0d;
    private Spinner spinner;
    private Spinner spinner2;
    /* access modifiers changed from: private */
    public TextToSpeech tts;
    /* access modifiers changed from: private */
    public String ttsengine;
    private SeekBar volumebar;

    /* access modifiers changed from: protected */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView((int) R.layout.activity_main);
        this.tts = new TextToSpeech(this, this, this.ttsengine);

        //this.editText = (TextView) findViewById(R.id.SMSText);
        this.deviceview = (TextView) findViewById(R.id.textView32);
        this.button1 = (Button) findViewById(R.id.button1);
        this.button2 = (Button) findViewById(R.id.button2);
        this.button3 = (Button) findViewById(R.id.button3);
        this.button4 = (Button) findViewById(R.id.button4);
        this.button5 = (Button) findViewById(R.id.button5);
        this.spinner = (Spinner) findViewById(R.id.spinner);
        this.spinner2 = (Spinner) findViewById(R.id.spinner2);
        this.volumebar = (SeekBar) findViewById(R.id.volumeBar);
        this.audioManager = (AudioManager) getSystemService("audio");
        this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        processIntent(getIntent());
        initializeKakaoReceiver();
        Regex(this.tts.getEngines().toString());

        int permission = ContextCompat.checkSelfPermission(this, "android.permission.RECEIVE_SMS");

        if (permission == 0) {
            Toast.makeText(this, "메시지 수신 권한 있음.", 1).show();
            this.button3.setBackgroundResource(R.drawable.on);
        } else {
            Toast.makeText(this, "메시지 수신 권한 없음.", 1).show();
            ActivityCompat.requestPermissions(this, new String[]{"android.permission.RECEIVE_SMS"}, 1);
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, "android.permission.RECEIVE_SMS")) {
                Toast.makeText(this, "메시지 권한 설명 필요함.", 1).show();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{"android.permission.RECEIVE_SMS"}, 1);
            }
        }

        this.button1.setTag(Integer.valueOf(permission));
        this.button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (MainActivity.this.button1.getBackground().getConstantState().equals(MainActivity.this.getResources().getDrawable(R.drawable.stop).getConstantState())) {
                    MainActivity.this.button1.setBackgroundResource(R.drawable.start);
                } else if (((Integer) view.getTag()).intValue() == 0) {
                    MainActivity.this.button1.setBackgroundResource(R.drawable.stop);
                }
            }
        });

        //권한 허가창 열기
        this.button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS", Uri.fromParts("package", MainActivity.this.getPackageName(), (String) null));
                intent.addFlags(268435456);
                MainActivity.this.startActivity(intent);
            }
        });

        //블루투스 연결창 열기
        this.button4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                MainActivity.this.startActivity(new Intent("android.settings.BLUETOOTH_SETTINGS"));
            }
        });

        //TTS 속도조정
        this.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                int selectedItemPosition = parent.getSelectedItemPosition();
                if (selectedItemPosition == 0) {
                    double unused = MainActivity.this.speed = 0.5d;
                } else if (selectedItemPosition == 1) {
                    double unused2 = MainActivity.this.speed = 1.0d;
                } else if (selectedItemPosition == 2) {
                    double unused3 = MainActivity.this.speed = 1.5d;
                } else if (selectedItemPosition == 3) {
                    double unused4 = MainActivity.this.speed = 2.0d;
                }

                Log.d("spinner", "speed : " + MainActivity.this.speed);
            }

            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        this.spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                MainActivity mainActivity = MainActivity.this;
                String unused = mainActivity.ttsengine = mainActivity.engineList.get(i);
                MainActivity.this.tts.stop();
                MainActivity mainActivity2 = MainActivity.this;
                TextToSpeech unused2 = mainActivity2.tts = new TextToSpeech(mainActivity2, mainActivity2, mainActivity2.ttsengine);
            }

            public void onNothingSelected(AdapterView<?> adapterView) {
                MainActivity mainActivity = MainActivity.this;
                TextToSpeech unused = mainActivity.tts = new TextToSpeech(mainActivity, mainActivity, mainActivity.tts.getDefaultEngine());
            }
        });

        this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        BluetoothAdapter bluetoothAdapter2 = this.bluetoothAdapter;
        if (bluetoothAdapter2 != null) {
            if (bluetoothAdapter2.isEnabled()) {
                this.button5.setBackgroundResource(R.drawable.on);
            } else {
                this.button5.setBackgroundResource(R.drawable.off);
                startActivityForResult(new Intent("android.bluetooth.adapter.action.REQUEST_ENABLE"), 10);
            }
        }
        registerReceiver(this.bluetoothReceiver, new IntentFilter("android.bluetooth.device.action.ACL_CONNECTED"));
        registerReceiver(this.bluetoothReceiver, new IntentFilter("android.bluetooth.device.action.ACL_DISCONNECTED"));
        registerReceiver(this.kakaoReceiver, new IntentFilter("android.service.notification.NotificationListenerService"));
        setSeekBar();
        if (!permissionGrantred()) {
            startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS));
        }
    }


    public void processIntent(Intent intent) {
        if (intent != null) {
            String sender = intent.getStringExtra("sender");
            String contents = intent.getStringExtra("contents");
            String date2 = intent.getStringExtra("receivedDate");

            if (sender == null || contents == null) {
                this.editText.setText((CharSequence) null);
            } else {
                TextView textView = this.editText;
                textView.setText(sender + 10 + contents + 10);
                moveTaskToBack(true);
            }
            if (!this.button1.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.stop).getConstantState())) {
                return;
            }

        }
    }

    public void onInit(int status) {
        if (status == 0) {
            int language = this.tts.setLanguage(Locale.KOREAN);
            if (language == -1 || language == -2) {
                this.editText.setText("지원하지 않는 언어입니다.");
            } else {
                this.isTTSReady = status;
            }
        } else {
            this.editText.setText("TTS 작업이 실패하였습니다.");
        }
    }

    private void initializeKakaoReceiver() {
        this.kakaoReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals("android.service.notification.NotificationListenerService")) {
                    String kTitle = intent.getExtras().getString("realTitle");
                    CharSequence tempKBody = intent.getExtras().getCharSequence("realBody");
                    String KBody = tempKBody.toString();
                    if (kTitle != null || KBody != null) {
                        Intent intent1 = new Intent();
                        intent1.putExtra("sender", kTitle);
                        intent1.putExtra("contents", tempKBody);
                        intent1.putExtra("class", "KAKAO");
                        MainActivity.this.processIntent(intent1);
                    }
                }
            }
        };
    }

    public void Regex(String str) {
        Matcher m = Pattern.compile("name=[A-za-z.]+").matcher(str);
        this.engineList = new ArrayList<>();
        this.engineSpinnerList = new ArrayList<>();
        int i = 0;
        while (m.find()) {
            this.engineList.add(m.group().substring(5));
            if (m.group().substring(5).equals("com.google.android.tts")) {
                this.engineSpinnerList.add("구글TTS");
            } else if (m.group().substring(5).equals("com.samsung.SMT")) {
                this.engineSpinnerList.add("삼성TTS");
            } else {
                this.engineSpinnerList.add(this.engineList.get(i));
            }
            i++;
        }
        this.arrayAdapter = new ArrayAdapter<>(getApplicationContext(), 17367049, this.engineSpinnerList);
        this.spinner2.setAdapter(this.arrayAdapter);
    }

    public void setSeekBar() {
        int nMax = this.audioManager.getStreamMaxVolume(3);
        int nCurrentVolume = this.audioManager.getStreamVolume(3);
        this.volumebar.setMax(nMax);
        this.volumebar.setProgress(nCurrentVolume);
        this.volumebar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                MainActivity.this.audioManager.setStreamVolume(3, progress, 0);
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }

    public void onNewIntent(Intent intent) {
        processIntent(intent);
        initializeKakaoReceiver();
        super.onNewIntent(intent);
    }

    private boolean permissionGrantred() {
        Set<String> sets = NotificationManagerCompat.getEnabledListenerPackages(this);
        if (sets == null || !sets.contains(getPackageName())) {
            return false;
        }
        return true;
    }

    public void onRestart() {
        super.onRestart();
        if (!permissionGrantred()) {
            startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS));
        }
        int permission = ContextCompat.checkSelfPermission(this, "android.permission.RECEIVE_SMS");
        if (permission == 0) {
            Toast.makeText(this, "메시지 수신 권한 주어져 있음.", 1).show();
            this.button3.setBackgroundResource(R.drawable.on);
        } else {
            Toast.makeText(this, "메시지 수신 권한 없음.", 1).show();
            ActivityCompat.requestPermissions(this, new String[]{"android.permission.RECEIVE_SMS"}, 1);
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, "android.permission.RECEIVE_SMS")) {
                Toast.makeText(this, "메시지 권한 설명 필요함.", 1).show();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{"android.permission.RECEIVE_SMS"}, 1);
            }
        }
        this.button1.setTag(Integer.valueOf(permission));
        this.button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (MainActivity.this.button1.getBackground().getConstantState().equals(MainActivity.this.getResources().getDrawable(R.drawable.stop).getConstantState())) {
                    MainActivity.this.button1.setBackgroundResource(R.drawable.start);
                } else if (((Integer) view.getTag()).intValue() == 0) {
                    MainActivity.this.button1.setBackgroundResource(R.drawable.stop);
                }
            }
        });
        this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        BluetoothAdapter bluetoothAdapter2 = this.bluetoothAdapter;
        if (bluetoothAdapter2 == null) {
            this.button5.setBackgroundResource(R.drawable.off);
        } else if (bluetoothAdapter2.isEnabled()) {
            this.button5.setBackgroundResource(R.drawable.on);
        } else {
            this.button5.setBackgroundResource(R.drawable.off);
            startActivityForResult(new Intent("android.bluetooth.adapter.action.REQUEST_ENABLE"), 10);
        }
        setSeekBar();
    }


    public void TextToSpeech(String str) {
        if (this.isTTSReady == 0) {
            this.tts.setPitch(1.0f);
            this.tts.setSpeechRate((float) this.speed);
            if (Build.VERSION.SDK_INT >= 21) {
                this.tts.addEarcon("[alarm]", BuildConfig.APPLICATION_ID, R.raw.alarm);
                this.tts.playEarcon("[alarm]", 1, (Bundle) null, "utteranceId");
                this.tts.speak(str, 1, (Bundle) null, "utteranceId");
                if (this.tts.isSpeaking()) {
                    Log.d("TEST", "SUCCESS");
                    return;
                }
                return;
            }
            HashMap<String, String> map = new HashMap<>();
            map.put("utteranceId", "UniqueID");
            this.tts.speak(str, 1, map);
        }
    }

    //메시지 수신권한 질의결과
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode != 1 || grantResults.length <= 0) {
            return;
        }
        if (grantResults[0] == 0) {
            Toast.makeText(this, "메시지 수신권한을 사용자가 승인함", 1).show();
            this.button3.setBackgroundResource(R.drawable.on);
            this.button1.setBackgroundResource(R.drawable.stop);
        } else if (grantResults[0] == -1) {
            Toast.makeText(this, "메시지 수신권한을 사용자가 거부함", 1).show();
            this.button3.setBackgroundResource(R.drawable.off);
            this.button1.setBackgroundResource(R.drawable.start);
        } else {
            Toast.makeText(this, "메시지 수신권한을 부여받지 못함", 1).show();
            this.button3.setBackgroundResource(R.drawable.off);
            this.button1.setBackgroundResource(R.drawable.start);
        }
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10 && resultCode == 0) {
            this.button5.setBackgroundResource(R.drawable.off);
        } else if (requestCode == 10 && resultCode == -1) {
            this.button5.setBackgroundResource(R.drawable.on);
        }
    }

    //종료

    public void onDestroy() {
        TextToSpeech textToSpeech = this.tts;
        if (textToSpeech != null) {
            textToSpeech.stop();
            this.tts.shutdown();
        }
        super.onDestroy();
    }
}