1616
1717package nie .translator .rtranslator .voice_translation ;
1818
19- import android .Manifest ;
2019import android .app .Notification ;
2120import android .app .PendingIntent ;
2221import android .content .Context ;
2524import android .content .SharedPreferences ;
2625import android .content .pm .PackageManager ;
2726import android .content .res .Configuration ;
28- import android .os .Build ;
2927import android .os .Bundle ;
3028import android .os .Handler ;
3129import android .os .Looper ;
32- import android .provider .Settings ;
3330import android .view .Menu ;
3431import android .view .MenuItem ;
3532import android .view .View ;
5047import androidx .fragment .app .Fragment ;
5148import androidx .fragment .app .FragmentManager ;
5249import androidx .fragment .app .FragmentTransaction ;
50+
51+ import com .google .android .material .dialog .MaterialAlertDialogBuilder ;
52+
5353import java .util .ArrayList ;
5454import java .util .List ;
5555import nie .translator .rtranslator .GeneralActivity ;
@@ -86,7 +86,6 @@ public class VoiceTranslationActivity extends GeneralActivity {
8686 public static final int DEFAULT_FRAGMENT = TRANSLATION_FRAGMENT ;
8787 //objects
8888 private Global global ;
89- private Fragment fragment ;
9089 private CoordinatorLayout fragmentContainer ;
9190 private int currentFragment = -1 ;
9291 private boolean startingPairing = false ; //used to start Pairing Mode after bluetooth permissions are granted
@@ -139,7 +138,8 @@ protected void onStart() {
139138 super .onStart ();
140139 // when we return to the app's gui based on the service that was saved in the last closure we choose which fragment to start
141140 SharedPreferences sharedPreferences = this .getSharedPreferences ("default" , Context .MODE_PRIVATE );
142- setFragment (sharedPreferences .getInt ("fragment" , DEFAULT_FRAGMENT ));
141+ int fragment = sharedPreferences .getInt ("fragment" , DEFAULT_FRAGMENT );
142+ setFragment (fragment , fragment == WALKIE_TALKIE_FRAGMENT || fragment == PAIRING_FRAGMENT );
143143 if (getResources () != null ) {
144144 config = getResources ().getConfiguration ();
145145 }
@@ -165,14 +165,18 @@ public boolean onCreateOptionsMenu(Menu menu) {
165165 }
166166
167167 public void setFragment (int fragmentName ) {
168+ setFragment (fragmentName , false );
169+ }
170+
171+ public void setFragment (int fragmentName , boolean requestPermission ) {
168172 switch (fragmentName ) {
169173 case PAIRING_FRAGMENT : {
170174 // possible stop of the Conversation and WalkieTalkie Service
171175 stopConversationService ();
172176 stopWalkieTalkieService ();
173177 // possible setting of the fragment
174- if (getCurrentFragment () != PAIRING_FRAGMENT ) {
175- if (Tools .hasPermissions (this , Global .REQUIRED_PERMISSIONS_PAIRING )) {
178+ if (getCurrentFragmentId () != PAIRING_FRAGMENT ) {
179+ if (Tools .hasPermissions (this , Global .REQUIRED_PERMISSIONS_PAIRING ) || ! requestPermission ) {
176180 global .initializeBluetoothCommunicator ();
177181 if (global .getBluetoothCommunicator () != null && global .getBluetoothCommunicator ().isBluetoothLeSupported ()){
178182 PairingFragment paringFragment = new PairingFragment ();
@@ -198,7 +202,7 @@ public void setFragment(int fragmentName) {
198202 }
199203 case CONVERSATION_FRAGMENT : {
200204 // possible setting of the fragment
201- if (getCurrentFragment () != CONVERSATION_FRAGMENT && global .getBluetoothCommunicator () != null ) {
205+ if (getCurrentFragmentId () != CONVERSATION_FRAGMENT && global .getBluetoothCommunicator () != null ) {
202206 ConversationFragment conversationFragment = new ConversationFragment ();
203207 Bundle bundle = new Bundle ();
204208 bundle .putBoolean ("firstStart" , true );
@@ -210,7 +214,7 @@ public void setFragment(int fragmentName) {
210214 currentFragment = CONVERSATION_FRAGMENT ;
211215 saveFragment ();
212216 // eventual permission request
213- if (Tools .hasPermissions (this , Global .REQUIRED_PERMISSIONS_VOICE )) {
217+ if (! Tools .hasPermissions (this , Global .REQUIRED_PERMISSIONS_VOICE ) && requestPermission ) {
214218 showPermissionDialog (CONVERSATION_FRAGMENT );
215219 }
216220 //fragment= conversationFragment;
@@ -221,8 +225,8 @@ public void setFragment(int fragmentName) {
221225 }
222226 case WALKIE_TALKIE_FRAGMENT : {
223227 // possible setting of the fragment
224- if (getCurrentFragment () != WALKIE_TALKIE_FRAGMENT ) {
225- if (Tools .hasPermissions (this , Global .REQUIRED_PERMISSIONS_VOICE )) {
228+ if (getCurrentFragmentId () != WALKIE_TALKIE_FRAGMENT ) {
229+ if (Tools .hasPermissions (this , Global .REQUIRED_PERMISSIONS_VOICE ) || ! requestPermission ) {
226230 WalkieTalkieFragment walkieTalkieFragment = new WalkieTalkieFragment ();
227231 FragmentTransaction transaction = getSupportFragmentManager ().beginTransaction ();
228232 Bundle bundle = new Bundle ();
@@ -245,7 +249,7 @@ public void setFragment(int fragmentName) {
245249 stopConversationService ();
246250 stopWalkieTalkieService ();
247251 // possible setting of the fragment
248- if (getCurrentFragment () != TRANSLATION_FRAGMENT ) {
252+ if (getCurrentFragmentId () != TRANSLATION_FRAGMENT ) {
249253 TranslationFragment translationFragment = new TranslationFragment ();
250254 FragmentTransaction transaction = getSupportFragmentManager ().beginTransaction ();
251255 Bundle bundle = new Bundle ();
@@ -270,13 +274,13 @@ public void run() {
270274 //save fragment
271275 SharedPreferences sharedPreferences = VoiceTranslationActivity .this .getSharedPreferences ("default" , Context .MODE_PRIVATE );
272276 SharedPreferences .Editor editor = sharedPreferences .edit ();
273- editor .putInt ("fragment" , getCurrentFragment ());
277+ editor .putInt ("fragment" , getCurrentFragmentId ());
274278 editor .apply ();
275279 }
276280 }.start ();
277281 }
278282
279- public int getCurrentFragment () {
283+ public int getCurrentFragmentId () {
280284 if (currentFragment != -1 ) {
281285 return currentFragment ;
282286 } else {
@@ -299,6 +303,10 @@ public int getCurrentFragment() {
299303 return -1 ;
300304 }
301305
306+ public Fragment getCurrentFragment () {
307+ return getSupportFragmentManager ().findFragmentById (R .id .fragment_container );
308+ }
309+
302310 @ Override
303311 public void onConfigurationChanged (@ NonNull Configuration newConfig ) {
304312 super .onConfigurationChanged (newConfig );
@@ -382,22 +390,23 @@ public void disconnect(Peer peer) {
382390 }
383391
384392
385- private void showPermissionDialog (int mode ){
393+ public void showPermissionDialog (int mode ){
386394 final View editDialogLayout = this .getLayoutInflater ().inflate (R .layout .dialog_permission , null );
387395
388- final AlertDialog . Builder builder = new AlertDialog . Builder (this );
396+ final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder (this , R . style . MyThemeOverlay_MaterialComponents_MaterialAlertDialog );
389397 builder .setCancelable (true );
390- //builder.setTitle("");
391398
392399 AlertDialog dialog = builder .create ();
393- if (dialog .getWindow () != null ) {
394- // Requests the window to have no title
395- dialog .getWindow ().requestFeature (Window .FEATURE_NO_TITLE );
396- // Removes the default system background and padding
397- dialog .getWindow ().setBackgroundDrawable (new android .graphics .drawable .ColorDrawable (android .graphics .Color .TRANSPARENT ));
398- }
399400 dialog .setView (editDialogLayout , 0 , Tools .convertDpToPixels (this , 16 ), 0 , 0 );
400401 dialog .show ();
402+ dialog .setOnDismissListener (new DialogInterface .OnDismissListener () {
403+ @ Override
404+ public void onDismiss (DialogInterface dialog ) {
405+ if (currentFragment == -1 ){
406+ setFragment (DEFAULT_FRAGMENT );
407+ }
408+ }
409+ });
401410
402411 ImageView icon = editDialogLayout .findViewById (R .id .dialogIcon );
403412 TextView text = editDialogLayout .findViewById (R .id .textView );
@@ -466,12 +475,17 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
466475 return ;
467476 }
468477
478+ Fragment fragment = getCurrentFragment ();
479+
469480 if (requestCode == Global .REQUEST_CODE_PERMISSIONS_PAIRING ){
470481 for (int grantResult : grantResults ) {
471482 if (grantResult == PackageManager .PERMISSION_DENIED ) {
472483 //notifyMissingSearchPermission();
473484 Toast .makeText (global , R .string .error_missing_location_permissions , Toast .LENGTH_LONG ).show ();
474485 startingPairing = false ;
486+ if (currentFragment == -1 ){
487+ setFragment (DEFAULT_FRAGMENT );
488+ }
475489 return ;
476490 }
477491 }
@@ -486,10 +500,16 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
486500 if (requestCode == Global .REQUEST_CODE_PERMISSIONS_WALKIETALKIE ){
487501 for (int grantResult : grantResults ) {
488502 if (grantResult == PackageManager .PERMISSION_DENIED ) {
503+ if (fragment instanceof VoiceTranslationFragment ) ((VoiceTranslationFragment ) fragment ).onMicPermissionResult (false );
489504 //todo: convert the string to resource and translate it
490505 Toast .makeText (global , "It is not possible to use the WalkieTalkie mode without microphone permission, go to the settings to grant it, or reinstall the app" , Toast .LENGTH_LONG ).show ();
491506 startingWalkieTalkie = false ;
507+ if (currentFragment == -1 ){
508+ setFragment (DEFAULT_FRAGMENT );
509+ }
492510 return ;
511+ }else {
512+ if (fragment instanceof VoiceTranslationFragment ) ((VoiceTranslationFragment ) fragment ).onMicPermissionResult (true );
493513 }
494514 }
495515 //mic permission is granted
@@ -500,11 +520,21 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
500520 }
501521
502522 if (requestCode == Global .REQUEST_CODE_PERMISSIONS_CONVERSATION ){
523+ ConversationMainFragment conversationFragment = null ;
524+ if (fragment instanceof ConversationFragment ){
525+ Fragment f = ((ConversationFragment ) fragment ).getCurrentFragment ();
526+ if (f instanceof ConversationMainFragment ){
527+ conversationFragment = (ConversationMainFragment ) f ;
528+ }
529+ }
503530 for (int grantResult : grantResults ) {
504531 if (grantResult == PackageManager .PERMISSION_DENIED ) {
532+ if (conversationFragment != null ) conversationFragment .onMicPermissionResult (false );
505533 //todo: convert the string to resource and translate it
506534 Toast .makeText (global , R .string .error_missing_mic_permissions , Toast .LENGTH_LONG ).show ();
507535 return ;
536+ }else {
537+ if (conversationFragment != null ) conversationFragment .onMicPermissionResult (true );
508538 }
509539 }
510540 //mic permission is granted
@@ -574,7 +604,6 @@ public void startConversationService(Notification notification, final Global.Res
574604 }
575605 startService (intent );
576606 if (responseListener != null ) responseListener .onSuccess ();
577-
578607 }
579608
580609 public void startWalkieTalkieService (Notification notification , final Global .ResponseListener responseListener ) {
0 commit comments