|
| 1 | +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.espressif; |
| 16 | + |
| 17 | +import android.content.BroadcastReceiver; |
| 18 | +import android.content.Context; |
| 19 | +import android.content.Intent; |
| 20 | +import android.os.Bundle; |
| 21 | +import android.text.TextUtils; |
| 22 | +import android.util.Log; |
| 23 | +import android.widget.Toast; |
| 24 | + |
| 25 | +import androidx.annotation.NonNull; |
| 26 | +import androidx.annotation.Nullable; |
| 27 | +import androidx.core.app.NotificationManagerCompat; |
| 28 | + |
| 29 | +import com.espressif.cloudapi.ApiManager; |
| 30 | +import com.espressif.cloudapi.ApiResponseListener; |
| 31 | +import com.espressif.cloudapi.CloudException; |
| 32 | +import com.espressif.rainmaker.R; |
| 33 | +import com.espressif.ui.activities.GroupDetailActivity; |
| 34 | +import com.espressif.ui.activities.SplashActivity; |
| 35 | + |
| 36 | +public class GroupSharingActionReceiver extends BroadcastReceiver { |
| 37 | + |
| 38 | + private static final String TAG = "GroupSharingReceiver"; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void onReceive(Context context, Intent intent) { |
| 42 | + |
| 43 | + String action = intent.getAction(); |
| 44 | + String requestId = intent.getStringExtra(AppConstants.KEY_REQ_ID); |
| 45 | + int notificationId = intent.getIntExtra(AppConstants.KEY_ID, 0); |
| 46 | + |
| 47 | + if (!TextUtils.isEmpty(requestId)) { |
| 48 | + |
| 49 | + if (AppConstants.ACTION_ACCEPT.equals(action)) { |
| 50 | + |
| 51 | + ApiManager.getInstance(context).updateGroupSharingRequest(requestId, true, new ApiResponseListener() { |
| 52 | + |
| 53 | + @Override |
| 54 | + public void onSuccess(@Nullable Bundle data) { |
| 55 | + |
| 56 | + Log.d(TAG, "Group sharing accepted."); |
| 57 | + NotificationManagerCompat.from(context).cancel(notificationId); |
| 58 | + Intent splashIntent = new Intent(context, SplashActivity.class); |
| 59 | + splashIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 60 | + context.startActivity(splashIntent); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onResponseFailure(@NonNull Exception exception) { |
| 65 | + |
| 66 | + if (exception instanceof CloudException) { |
| 67 | + Toast.makeText(context, exception.getMessage(), Toast.LENGTH_SHORT).show(); |
| 68 | + } else { |
| 69 | + Toast.makeText(context, "Failed to accept group sharing due to response failure", Toast.LENGTH_SHORT).show(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void onNetworkFailure(@NonNull Exception exception) { |
| 75 | + |
| 76 | + if (exception instanceof CloudException) { |
| 77 | + Toast.makeText(context, exception.getMessage(), Toast.LENGTH_SHORT).show(); |
| 78 | + } else { |
| 79 | + Toast.makeText(context, "Failed to accept group sharing due to network failure", Toast.LENGTH_SHORT).show(); |
| 80 | + } |
| 81 | + } |
| 82 | + }); |
| 83 | + } else if (AppConstants.ACTION_DECLINE.equals(action)) { |
| 84 | + |
| 85 | + ApiManager.getInstance(context).updateGroupSharingRequest(requestId, false, new ApiResponseListener() { |
| 86 | + @Override |
| 87 | + public void onSuccess(@Nullable Bundle data) { |
| 88 | + |
| 89 | + Log.d(TAG, "Group sharing declined."); |
| 90 | + NotificationManagerCompat.from(context).cancel(notificationId); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onResponseFailure(@NonNull Exception exception) { |
| 95 | + |
| 96 | + if (exception instanceof CloudException) { |
| 97 | + Toast.makeText(context, exception.getMessage(), Toast.LENGTH_SHORT).show(); |
| 98 | + } else { |
| 99 | + Toast.makeText(context, "Failed to decline group sharing due to response failure", Toast.LENGTH_SHORT).show(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onNetworkFailure(@NonNull Exception exception) { |
| 105 | + |
| 106 | + if (exception instanceof CloudException) { |
| 107 | + Toast.makeText(context, exception.getMessage(), Toast.LENGTH_SHORT).show(); |
| 108 | + } else { |
| 109 | + Toast.makeText(context, "Failed to decline group sharing due to network failure", Toast.LENGTH_SHORT).show(); |
| 110 | + } |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments