-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckList.java
More file actions
336 lines (299 loc) · 13.7 KB
/
Copy pathCheckList.java
File metadata and controls
336 lines (299 loc) · 13.7 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
package com.example.trip_packer;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.speech.RecognizerIntent;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.example.trip_packer.Data.AppData;
import com.example.trip_packer.Database.RoomDB;
import com.example.trip_packer.Models.Items;
import com.example.trip_packer.adapter.CheckListAdapter;
import com.example.trip_packer.constants.myconstants;
import java.util.ArrayList;
import java.util.List;
public class CheckList extends AppCompatActivity {
RecyclerView recyclerView;
CheckListAdapter checkListAdapter;
RoomDB database;
List<Items> itemsList = new ArrayList<>();
String header, show;
private static final int REQUEST_CODE_VOICE_RECOGNITION = 123;
private static final int MY_PERMISSIONS_REQUEST_RECORD_AUDIO=1;
private MyReceiver myReceiver;
EditText txtAdd;
Button btnAdd;
LinearLayout linearLayout;
@Override
public boolean onCreatePanelMenu(int featureid,@NonNull Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_1, menu);
if (myconstants.MY_SELECTIONS.equals(header)) {
menu.getItem(0).setVisible(false);
menu.getItem(2).setVisible(false);
menu.getItem(3).setVisible(false);
} else if (myconstants.MY_LIST_CAMEL_CASE.equals(header))
menu.getItem(1).setVisible(false);
menu.add(Menu.NONE, R.id.btnVoiceRecognition, Menu.NONE, "Voice Recognition")
.setIcon(R.drawable.ic_baseline_keyboard_voice_24)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
MenuItem menuItem = menu.findItem(R.id.btnSearch);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
List<Items> nFinalList = new ArrayList<>();
for (Items items : itemsList) {
if (items.getItemname().toLowerCase().startsWith(newText.toLowerCase())) {
nFinalList.add(items);
}
}
updateRecycler(nFinalList);
return false;
}
});
MenuItem shareItem = menu.findItem(R.id.btnshare);
shareItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Log.d("CheckList", "Share button clicked");
shareChecklist();
return true;
}
});
/*for (int i = 0; i < menu.size(); i++) {
MenuItem menuItem1 = menu.getItem(i);
if (menuItem.getActionView() != null) {
SpannableString spannableString = new SpannableString(menuItem.getTitle());
spannableString.setSpan(
new ForegroundColorSpan(getResources().getColor(R.color.menu_item_text_color)),
0,
spannableString.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
menuItem.setTitle(spannableString);
}
}*/
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
Intent intent = new Intent(this, CheckList.class);
AppData appData = new AppData(database, this);
switch (item.getItemId()) {
case R.id.btnMySelections:
intent.putExtra(myconstants.HEADER_SMALL, myconstants.MY_SELECTIONS);
intent.putExtra(myconstants.SHOW_SMALL, myconstants.FALSE_STRING);
startActivityForResult(intent, 101);
return true;
case R.id.btnCustomList:
intent.putExtra(myconstants.HEADER_SMALL, myconstants.MY_LIST_CAMEL_CASE);
intent.putExtra(myconstants.SHOW_SMALL, myconstants.TRUE_STRING);
startActivity(intent);
return true;
case R.id.btnDeleteDefault:
new AlertDialog.Builder(this)
.setTitle("Delete default data")
.setMessage("Are you sure?\n\n As this will delete the data provided by(trip packer) while installing.")
.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
appData.persistDataByCtaegory(header, true);
itemsList = database.mainDao().getAll(header);
updateRecycler(itemsList);
}
}).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).setIcon(R.drawable.ic_baseline_warning_24).show();
return true;
case R.id.btnReset:
new AlertDialog.Builder(this)
.setTitle("Reset to default")
.setMessage("Are you sure?\n\nAs this will load the default data provided by (trip packer)" +
" and will delete the custom data you have added in (" + header + ")")
.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
appData.persistDataByCtaegory(header, false);
itemsList = database.mainDao().getAll(header);
updateRecycler(itemsList);
}
}).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).setIcon(R.drawable.ic_baseline_warning_24).show();
return true;
case R.id.btnAboutus:
Intent aboutUsIntent = new Intent(this, AboutUsService.class);
startService(aboutUsIntent);
startActivity(new Intent(this, AboutUs.class));
return true;
case R.id.btnshare:
Intent shareIntent = new Intent(this, ShareService.class);
startService(shareIntent);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(CheckList.this, "ShareService started", Toast.LENGTH_SHORT).show();
shareChecklist();
}
},1000);
return true;
case R.id.btnExit:
this.finishAffinity();
Toast.makeText(this, "TRIP_PACKER\nExit completed", Toast.LENGTH_SHORT).show();
return true;
case R.id.btnVoiceRecognition:
startVoiceRecognition();
startMyService();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void startMyService() {
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_VOICE_RECOGNITION && resultCode == RESULT_OK && data != null) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (results != null && !results.isEmpty()) {
String spokenText = results.get(0);
addNewitem(spokenText);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_list);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
header = intent.getStringExtra(myconstants.HEADER_SMALL);
show = intent.getStringExtra(myconstants.SHOW_SMALL);
getSupportActionBar().setTitle(header);
txtAdd = findViewById(R.id.txtAdd);
btnAdd = findViewById(R.id.btnAdd);
recyclerView = findViewById(R.id.recyclerview);
linearLayout = findViewById(R.id.linearlayout);
database = RoomDB.getInstance(this);
if (myconstants.FALSE_STRING.equals(show)) {
linearLayout.setVisibility(View.GONE);
itemsList = database.mainDao().getAllselected(true);
} else {
itemsList = database.mainDao().getAll(header);
}
updateRecycler(itemsList);
Button btnVoiceRecognition = findViewById(R.id.btnVoiceRecognition);
btnVoiceRecognition.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startVoiceRecognition();
}
});
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String itemName = txtAdd.getText().toString();
if (itemName != null && !itemName.isEmpty()) {
addNewitem(itemName);
Toast.makeText(CheckList.this, "Item added", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(CheckList.this, "Empty can't be added", Toast.LENGTH_SHORT).show();
}
}
});
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.trip_packer.SERVICE_STARTED");
intentFilter.addAction("com.example.trip_packer.SERVICE_STOPPED");
registerReceiver(myReceiver,intentFilter);
}
protected void onDestroy() {
// Unregister the BroadcastReceiver when the activity is destroyed
unregisterReceiver(myReceiver);
Intent serviceIntent = new Intent(this, MyService.class);
stopService(serviceIntent);
super.onDestroy();
}
private void addNewitem(String itemname) {
Items item = new Items();
item.setChecked(true);
item.setCategory(header);
item.setItemname(itemname);
item.setAddedby(myconstants.USER_SMALL);
database.mainDao().saveItem(item);
itemsList = database.mainDao().getAll(header);
updateRecycler(itemsList);
recyclerView.scrollToPosition(checkListAdapter.getItemCount() - 1);
txtAdd.setText("");
}
private void updateRecycler(List<Items> itemsList) {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL));
checkListAdapter = new CheckListAdapter(CheckList.this, itemsList, database, show);
recyclerView.setAdapter(checkListAdapter);
}
private void shareChecklist() {
StringBuilder sharedText = new StringBuilder("Checklist Items:\n");
for (Items item : itemsList) {
sharedText.append(item.getItemname()).append("\n");
}
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, sharedText.toString());
startActivity(Intent.createChooser(shareIntent, "Share Checklist"));
}
private void startVoiceRecognition() {
/* if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},
MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
} else {*/
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak now");
try {
startActivityForResult(intent, REQUEST_CODE_VOICE_RECOGNITION);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Voice recognition not supported on this device", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
}