• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.receivecontent;
18 
19 import android.net.Uri;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.view.Menu;
23 import android.view.MenuInflater;
24 import android.view.MenuItem;
25 import android.widget.EditText;
26 import android.widget.LinearLayout;
27 
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 import androidx.appcompat.app.AppCompatActivity;
31 import androidx.appcompat.widget.Toolbar;
32 import androidx.recyclerview.widget.RecyclerView;
33 
34 import com.google.common.collect.ImmutableList;
35 import com.google.common.util.concurrent.FutureCallback;
36 import com.google.common.util.concurrent.Futures;
37 import com.google.common.util.concurrent.ListenableFuture;
38 
39 /** Main activity for the demo. */
40 public class ReceiveContentDemoActivity extends AppCompatActivity {
41     private AttachmentsRepo mAttachmentsRepo;
42     private AttachmentsRecyclerViewAdapter mAttachmentsRecyclerViewAdapter;
43 
44     @Override
onCreate(@ullable Bundle savedInstanceState)45     protected void onCreate(@Nullable Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47         setContentView(R.layout.activity_main);
48 
49         // Setup the app toolbar.
50         Toolbar toolbar = findViewById(R.id.app_toolbar);
51         setSupportActionBar(toolbar);
52 
53         // Setup the repository and recycler view for attachments.
54         mAttachmentsRepo = new AttachmentsRepo(this);
55         ImmutableList<Uri> attachments = mAttachmentsRepo.getAllUris();
56         RecyclerView attachmentsRecyclerView = findViewById(R.id.attachments_recycler_view);
57         attachmentsRecyclerView.setHasFixedSize(true);
58         mAttachmentsRecyclerViewAdapter = new AttachmentsRecyclerViewAdapter(attachments);
59         attachmentsRecyclerView.setAdapter(mAttachmentsRecyclerViewAdapter);
60 
61         // Setup the listener for receiving content.
62         MyReceiver receiver = new MyReceiver(mAttachmentsRepo, mAttachmentsRecyclerViewAdapter);
63         LinearLayout container = findViewById(R.id.container);
64         container.setOnReceiveContentListener(MyReceiver.SUPPORTED_MIME_TYPES, receiver);
65         EditText textInput = findViewById(R.id.text_input);
66         textInput.setOnReceiveContentListener(MyReceiver.SUPPORTED_MIME_TYPES, receiver);
67     }
68 
69     @Override
onCreateOptionsMenu(@onNull Menu menu)70     public boolean onCreateOptionsMenu(@NonNull Menu menu) {
71         MenuInflater inflater = getMenuInflater();
72         inflater.inflate(R.menu.app_menu, menu);
73         return true;
74     }
75 
76     @Override
onOptionsItemSelected(@onNull MenuItem item)77     public boolean onOptionsItemSelected(@NonNull MenuItem item) {
78         if (item.getItemId() == R.id.action_clear_attachments) {
79             deleteAllAttachments();
80             return true;
81         }
82         return false;
83     }
84 
deleteAllAttachments()85     private void deleteAllAttachments() {
86         ListenableFuture<Void> deleteAllFuture = MyExecutors.bg().submit(() -> {
87             mAttachmentsRepo.deleteAll();
88             return null;
89         });
90         Futures.addCallback(deleteAllFuture, new FutureCallback<Void>() {
91             @Override
92             public void onSuccess(@Nullable Void result) {
93                 mAttachmentsRecyclerViewAdapter.clearAttachments();
94                 mAttachmentsRecyclerViewAdapter.notifyDataSetChanged();
95             }
96             @Override
97             public void onFailure(@NonNull Throwable t) {
98                 Log.e(Logcat.TAG, "Error deleting attachments", t);
99             }
100         }, MyExecutors.main());
101     }
102 }
103