-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainActivity.java
More file actions
138 lines (117 loc) · 4.73 KB
/
MainActivity.java
File metadata and controls
138 lines (117 loc) · 4.73 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
/*
* Copyright 2016-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.amazonaws.sample.lex;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document;
import com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive;
import java.util.List;
import com.amazonaws.sample.lex.R;
public class MainActivity extends Activity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private static final int REQUEST_RECORDING_PERMISSIONS_RESULT = 75;
private Button textDemoButton;
private Button speechDemoButton;
private class GetAllItemsAsyncTask extends AsyncTask<Void, Void, List<Document>> {
@Override
protected List<Document> doInBackground(Void... params) {
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(MainActivity.this);
Log.d(TAG, "databases content"+databaseAccess.getAllItems().toString());
return databaseAccess.getAllItems();
}
@Override
protected void onPostExecute(List<Document> documents) {
}
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "Trying to db connect here ");
init();
}
@Override
public void onBackPressed() {
finish();
moveTaskToBack(true);
}
/**
* Initializes the application.
*/
private void init() {
Log.e(TAG, "Initializing app");
Log.e(TAG, BuildConfig.API_URL );
textDemoButton = (Button) findViewById(R.id.button_select_text);
speechDemoButton = (Button) findViewById(R.id.button_select_voice);
textDemoButton.setOnClickListener(this);
speechDemoButton.setOnClickListener(this);
// Starting with Marshmallow we need to explicitly ask if we can record audio
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) ==
PackageManager.PERMISSION_GRANTED) {
speechDemoButton.setEnabled(true);
} else {
requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_RECORDING_PERMISSIONS_RESULT);
}
} else {
speechDemoButton.setEnabled(true);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_RECORDING_PERMISSIONS_RESULT) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),
"LexSample will not be able to use the voice feature", Toast.LENGTH_SHORT).show();
// Disable the button
speechDemoButton.setEnabled(false);
} else {
speechDemoButton.setEnabled(true);
}
}
}
/**
* On-click listener for buttons text and voice buttons.
*
* @param v {@link View}, instance of the button component.
*/
@Override
public void onClick(final View v) {
switch ((v.getId())) {
case R.id.button_select_text:
Intent textIntent = new Intent(this, TextActivity.class);
// startActivity(textIntent);
Log.e(TAG, "Connecting to DB onclick ");
GetAllItemsAsyncTask getAllDevicesTask = new GetAllItemsAsyncTask();
getAllDevicesTask.execute();
break;
case R.id.button_select_voice:
Intent voiceIntent = new Intent(this, InteractiveVoiceActivity.class);
startActivity(voiceIntent);
break;
}
}
}