10package org.libreoffice.ui;
12import android.Manifest;
13import android.content.ActivityNotFoundException;
14import android.content.ComponentName;
15import android.content.Context;
16import android.content.Intent;
17import android.content.SharedPreferences;
18import android.content.pm.PackageManager;
19import android.content.pm.ShortcutInfo;
20import android.content.pm.ShortcutManager;
21import android.graphics.drawable.Icon;
22import android.net.Uri;
23import android.os.Build;
24import android.os.Bundle;
25import android.preference.PreferenceManager;
26import com.google.android.material.floatingactionbutton.FloatingActionButton;
27import androidx.core.app.ActivityCompat;
28import androidx.core.content.ContextCompat;
29import androidx.core.view.ViewCompat;
30import androidx.appcompat.app.ActionBar;
31import androidx.appcompat.app.AppCompatActivity;
32import androidx.recyclerview.widget.GridLayoutManager;
33import androidx.recyclerview.widget.RecyclerView;
34import androidx.appcompat.widget.Toolbar;
35import android.text.TextUtils;
36import android.util.Log;
37import android.view.Menu;
38import android.view.MenuInflater;
39import android.view.MenuItem;
40import android.view.View;
41import android.view.animation.Animation;
42import android.view.animation.AnimationUtils;
43import android.view.animation.OvershootInterpolator;
44import android.widget.LinearLayout;
45import android.widget.TextView;
46import android.widget.Toast;
56import java.util.ArrayList;
57import java.util.Arrays;
85 "application/vnd.oasis.opendocument.text",
86 "application/vnd.oasis.opendocument.graphics",
87 "application/vnd.oasis.opendocument.presentation",
88 "application/vnd.oasis.opendocument.spreadsheet",
89 "application/vnd.oasis.opendocument.text-flat-xml",
90 "application/vnd.oasis.opendocument.graphics-flat-xml",
91 "application/vnd.oasis.opendocument.presentation-flat-xml",
92 "application/vnd.oasis.opendocument.spreadsheet-flat-xml",
93 "application/vnd.oasis.opendocument.text-template",
94 "application/vnd.oasis.opendocument.spreadsheet-template",
95 "application/vnd.oasis.opendocument.graphics-template",
96 "application/vnd.oasis.opendocument.presentation-template",
100 "application/vnd.ms-powerpoint",
101 "application/vnd.ms-excel",
102 "application/vnd.visio",
103 "application/vnd.visio.xml",
104 "application/x-mspublisher",
105 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
106 "application/vnd.openxmlformats-officedocument.presentationml.presentation",
107 "application/vnd.openxmlformats-officedocument.presentationml.slideshow",
108 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
109 "application/vnd.openxmlformats-officedocument.wordprocessingml.template",
110 "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
111 "application/vnd.openxmlformats-officedocument.presentationml.template",
113 "text/comma-separated-values",
114 "application/vnd.ms-works",
115 "application/vnd.apple.keynote",
116 "application/x-abiword",
117 "application/x-pagemaker",
143 super.onCreate(savedInstanceState);
157 if (ContextCompat.checkSelfPermission(
this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
158 Log.i(
LOGTAG,
"no permission to read external storage - asking for permission");
159 ActivityCompat.requestPermissions(
this,
160 new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
171 setContentView(
R.layout.activity_document_browser);
173 Toolbar toolbar = findViewById(
R.id.toolbar);
174 setSupportActionBar(toolbar);
175 ActionBar actionBar = getSupportActionBar();
177 if (actionBar !=
null) {
178 actionBar.setIcon(
R.drawable.lo_icon);
181 editFAB = findViewById(
R.id.editFAB);
182 editFAB.setOnClickListener(
this);
185 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
187 final boolean bAllowCreatingDocs = bEditingEnabled && Build.VERSION.SDK_INT >= 19;
188 editFAB.setVisibility(bAllowCreatingDocs ?
View.VISIBLE :
View.INVISIBLE);
194 calcFAB = findViewById(
R.id.newCalcFAB);
195 calcFAB.setOnClickListener(
this);
196 drawFAB = findViewById(
R.id.newDrawFAB);
197 drawFAB.setOnClickListener(
this);
202 TextView openFileView = findViewById(
R.id.open_file_view);
203 openFileView.setOnClickListener(
this);
206 RecyclerView recentRecyclerView = findViewById(
R.id.list_recent);
212 final List<RecentFile> recentFiles =
new ArrayList<>();
213 for (
String recentFileString : recentFileStrings) {
214 Uri uri = Uri.parse(recentFileString);
216 if (!filename.isEmpty()) {
217 recentFiles.add(
new RecentFile(uri, filename));
221 recentRecyclerView.setLayoutManager(
new GridLayoutManager(
this, 2));
222 recentRecyclerView.setAdapter(
new RecentFilesAdapter(
this, recentFiles));
226 ViewCompat.animate(
editFAB).rotation(45.0
F).withLayer().setDuration(300).setInterpolator(
new OvershootInterpolator(10.0
F)).start();
239 ViewCompat.animate(
editFAB).rotation(0.0
F).withLayer().setDuration(300).setInterpolator(
new OvershootInterpolator(10.0
F)).start();
256 super.onBackPressed();
262 super.onActivityResult(requestCode, resultCode, data);
264 final Uri fileUri = data.getData();
270 Intent intent =
new Intent();
271 if (Build.VERSION.SDK_INT >= 19) {
272 intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
276 intent.setAction(Intent.ACTION_GET_CONTENT);
279 intent.setType(
"*/*");
284 }
catch (ActivityNotFoundException e) {
285 Log.w(
LOGTAG,
"No activity available that can handle the intent to open a document.");
291 Intent intent =
new Intent(Intent.ACTION_VIEW, documentUri);
292 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
296 String packageName = getApplicationContext().getPackageName();
297 ComponentName componentName =
new ComponentName(packageName,
299 intent.setComponent(componentName);
300 startActivity(intent);
304 final String newDocumentType;
314 Log.w(
LOGTAG,
"invalid document type passed to loadNewDocument method. Ignoring request");
320 startActivity(intent);
325 MenuInflater inflater = getMenuInflater();
326 inflater.inflate(
R.menu.view_menu, menu);
333 final int itemId = item.getItemId();
334 if (itemId ==
R.id.action_about) {
336 aboutDialogFragment.show(getSupportFragmentManager(),
"AboutDialogFragment");
339 if (itemId ==
R.id.action_settings) {
344 return super.onOptionsItemSelected(item);
348 SharedPreferences defaultPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
367 if (Build.VERSION.SDK_INT < 19) {
369 Log.i(
LOGTAG,
"Recently used files not supported, requires SDK version >= 19.");
377 getContentResolver().takePersistableUriPermission(fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
379 String newRecent = fileUri.toString();
383 recentsList.remove(newRecent);
386 recentsList.add(0, newRecent);
392 final int RECENTS_SIZE = 4;
394 while (recentsList.size() > RECENTS_SIZE) {
395 recentsList.remove(RECENTS_SIZE);
403 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N_MR1) {
404 ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
407 shortcutManager.removeAllDynamicShortcuts();
409 ArrayList<ShortcutInfo> shortcuts =
new ArrayList<>();
410 for (
String recentDoc : recentsList) {
411 Uri docUri = Uri.parse(recentDoc);
413 if (filename.isEmpty()) {
421 drawable =
R.drawable.writer;
424 drawable =
R.drawable.calc;
427 drawable =
R.drawable.draw;
430 drawable =
R.drawable.impress;
434 Intent intent =
new Intent(Intent.ACTION_VIEW, docUri);
435 String packageName = this.getApplicationContext().getPackageName();
437 intent.setComponent(componentName);
439 ShortcutInfo shortcut =
new ShortcutInfo.Builder(
this, filename)
440 .setShortLabel(filename)
441 .setLongLabel(filename)
442 .setIcon(Icon.createWithResource(
this, drawable))
446 shortcuts.add(shortcut);
448 shortcutManager.setDynamicShortcuts(shortcuts);
455 if (
id ==
R.id.editFAB) {
461 }
else if (
id ==
R.id.open_file_view) {
463 }
else if (
id ==
R.id.newWriterFAB) {
465 }
else if (
id ==
R.id.newImpressFAB) {
467 }
else if (
id ==
R.id.newCalcFAB) {
469 }
else if (
id ==
R.id.newDrawFAB) {
Main activity of the LibreOffice App.
static final String ENABLE_EXPERIMENTAL_PREFS_KEY
static Context onAttach(Context context)
static Context setLocale(Context context, String lang)
static final String SYSTEM_DEFAULT_LANGUAGE
void setListener(OnSettingsPreferenceChangedListener listener)
static SettingsListenerModel getInstance()
static String retrieveDisplayNameForDocumentUri(ContentResolver resolver, Uri docUri)
Tries to retrieve the display (which should be the document name) for the given URI using the given r...
static final String NEW_CALC_STRING_KEY
void onActivityResult(int requestCode, int resultCode, Intent data)
boolean onCreateOptionsMenu(Menu menu)
void openDocument(final Uri documentUri)
static final String NEW_DOC_TYPE_KEY
Animation fabOpenAnimation
static final String RECENT_DOCUMENTS_KEY
void loadNewDocument(DocumentType docType)
void onCreate(Bundle savedInstanceState)
LinearLayout impressLayout
LinearLayout writerLayout
FloatingActionButton drawFAB
static final int REQUEST_CODE_OPEN_FILECHOOSER
void attachBaseContext(Context newBase)
Animation fabCloseAnimation
FloatingActionButton calcFAB
static final String NEW_IMPRESS_STRING_KEY
static final String EXPLORER_PREFS_KEY
static final String RECENT_DOCUMENTS_DELIMITER
static final int PERMISSION_WRITE_EXTERNAL_STORAGE
static final String LOGTAG
void settingsPreferenceChanged(SharedPreferences sharedPreferences, String key)
void addDocumentToRecents(Uri fileUri)
static final String NEW_DRAW_STRING_KEY
boolean onOptionsItemSelected(MenuItem item)
FloatingActionButton writerFAB
static final String[] SUPPORTED_MIME_TYPES
FloatingActionButton editFAB
static final String NEW_WRITER_STRING_KEY
FloatingActionButton impressFAB
static final String DISPLAY_LANGUAGE
void showSystemFilePickerAndOpenFile()
An entry for a recently used file in the RecentFilesAdapter.