Add default launch view setting

This commit is contained in:
William Brawner 2018-02-02 18:36:49 -06:00 committed by William Brawner
parent 7796adc55f
commit 28dc2d4ba7
8 changed files with 105 additions and 60 deletions

View file

@ -121,6 +121,7 @@ public class MarkdownPresenterImpl implements MarkdownPresenter {
listener.saveComplete(result);
}
if (editView != null) {
editView.setTitle(file.getName());
editView.onFileSaved(result);
}
};

View file

@ -1,9 +1,12 @@
package com.wbrawner.simplemarkdown.utility;
import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import com.wbrawner.simplemarkdown.view.activity.SettingsActivity;
@ -85,4 +88,11 @@ public class Utils {
}
}
}
public static boolean canAccessFiles(Context context) {
return ContextCompat.checkSelfPermission(
context,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED;
}
}

View file

@ -41,6 +41,7 @@ public class MainActivity extends AppCompatActivity
static final String EXTRA_FILE = "EXTRA_FILE";
static final String EXTRA_FILE_PATH = "EXTRA_FILE_PATH";
static final String EXTRA_REQUEST_CODE = "EXTRA_REQUEST_CODE";
static final String EXTRA_EXPLORER = "EXTRA_EXPLORER";
@Inject
MarkdownPresenter presenter;
@ -70,6 +71,9 @@ public class MainActivity extends AppCompatActivity
tabLayout.setVisibility(View.GONE);
}
newFileHandler = new NewFileHandler();
if (getIntent().getBooleanExtra(EXTRA_EXPLORER, false)) {
requestFileOp(OPEN_FILE_REQUEST);
}
}
@Override
@ -99,19 +103,7 @@ public class MainActivity extends AppCompatActivity
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_save:
if (ContextCompat.checkSelfPermission(
MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED)
requestSave();
else {
if (Build.VERSION.SDK_INT >= 23) {
requestPermissions(
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
WRITE_PERMISSION_REQUEST
);
}
}
requestFileOp(SAVE_FILE_REQUEST);
break;
case R.id.action_share:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
@ -123,19 +115,7 @@ public class MainActivity extends AppCompatActivity
));
break;
case R.id.action_load:
if (ContextCompat.checkSelfPermission(
MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED)
requestOpen();
else {
if (Build.VERSION.SDK_INT >= 23) {
requestPermissions(
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
OPEN_FILE_REQUEST
);
}
}
requestFileOp(OPEN_FILE_REQUEST);
break;
case R.id.action_new:
presenter.saveMarkdown(newFileHandler, null);
@ -204,24 +184,13 @@ public class MainActivity extends AppCompatActivity
@NonNull int[] grantResults
) {
switch (requestCode) {
case WRITE_PERMISSION_REQUEST: {
case SAVE_FILE_REQUEST:
case OPEN_FILE_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, open file save dialog
requestSave();
} else {
// Permission denied, do nothing
Toast.makeText(MainActivity.this, R.string.no_permissions, Toast.LENGTH_SHORT)
.show();
}
break;
}
case OPEN_FILE_REQUEST: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, open file save dialog
requestOpen();
requestFileOp(requestCode);
} else {
// Permission denied, do nothing
Toast.makeText(MainActivity.this, R.string.no_permissions, Toast.LENGTH_SHORT)
@ -260,29 +229,28 @@ public class MainActivity extends AppCompatActivity
}
String path = data.getStringExtra(EXTRA_FILE_PATH);
presenter.saveMarkdown(null, path);
setTitle(presenter.getFileName());
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void requestOpen() {
Intent intent = new Intent(MainActivity.this, ExplorerActivity.class);
intent.putExtra(EXTRA_REQUEST_CODE, OPEN_FILE_REQUEST);
startActivityForResult(
intent,
OPEN_FILE_REQUEST
);
}
private void requestSave() {
Intent intent = new Intent(MainActivity.this, ExplorerActivity.class);
intent.putExtra(EXTRA_REQUEST_CODE, SAVE_FILE_REQUEST);
intent.putExtra(EXTRA_FILE, presenter.getFile());
startActivityForResult(
intent,
SAVE_FILE_REQUEST
);
private void requestFileOp(int requestType) {
if (Utils.canAccessFiles(MainActivity.this)) {
Intent intent = new Intent(MainActivity.this, ExplorerActivity.class);
intent.putExtra(EXTRA_REQUEST_CODE, requestType);
intent.putExtra(EXTRA_FILE, presenter.getFile());
startActivityForResult(
intent,
requestType
);
} else {
if (Build.VERSION.SDK_INT >= 23) {
requestPermissions(
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
requestType
);
}
}
}
@Override

View file

@ -11,6 +11,8 @@ public class SettingsActivity extends AppCompatActivity {
public static final String KEY_AUTOSAVE = "autosave";
public static final String KEY_DOCS_PATH = "defaultRootDir";
public static final String EDIT_VIEW = "0";
public static final String FILE_VIEW = "1";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

View file

@ -2,10 +2,12 @@ package com.wbrawner.simplemarkdown.view.activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.wbrawner.simplemarkdown.MarkdownApplication;
import com.wbrawner.simplemarkdown.R;
import com.wbrawner.simplemarkdown.presentation.MarkdownPresenter;
import com.wbrawner.simplemarkdown.utility.Utils;
@ -30,7 +32,18 @@ public class SplashActivity extends AppCompatActivity {
presenter.setFileName(defaultName);
}
startActivity(new Intent(this, MainActivity.class));
Intent startIntent = new Intent(this, MainActivity.class);
String startScreen = PreferenceManager.getDefaultSharedPreferences(this)
.getString(
getString(R.string.key_default_view),
SettingsActivity.EDIT_VIEW
);
switch (startScreen) {
case SettingsActivity.FILE_VIEW:
startIntent.putExtra(MainActivity.EXTRA_EXPLORER, true);
break;
}
startActivity(startIntent);
finish();
}
}

View file

@ -1,15 +1,47 @@
package com.wbrawner.simplemarkdown.view.fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import com.wbrawner.simplemarkdown.R;
public class SettingsFragment extends PreferenceFragment {
public class SettingsFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(getActivity());
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
setListPreferenceSummary(
sharedPreferences,
findPreference(getString(R.string.key_default_view))
);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Preference preference = findPreference(key);
if (preference instanceof ListPreference) {
setListPreferenceSummary(sharedPreferences, preference);
}
}
private void setListPreferenceSummary(SharedPreferences sharedPreferences, Preference preference) {
ListPreference listPreference = (ListPreference) preference;
String storedValue = sharedPreferences.getString(preference.getKey(), "");
if (storedValue.isEmpty()) {
return;
}
int index = Integer.valueOf(storedValue);
String summary = listPreference.getEntries()[index].toString();
preference.setSummary(summary);
}
}

View file

@ -33,4 +33,17 @@
<string name="pref_title_autosave">Enable autosave</string>
<string name="pref_description_autosave">Automatically save files when closing the app</string>
<string name="action_lock_swipe">Lock Swiping</string>
<string name="key_default_view">defaultView</string>
<string name="pref_description_default_view">Decide what you see first when you open the app</string>
<string name="pref_title_default_view">Default View</string>
<string name="value_edit_view">Edit View</string>
<string name="value_explorer_view">File Explorer View</string>
<string-array name="pref_entries_default_view">
<item>@string/value_edit_view</item>
<item>@string/value_explorer_view</item>
</string-array>
<string-array name="pref_values_default_view">
<item>0</item>
<item>1</item>
</string-array>
</resources>

View file

@ -5,5 +5,11 @@
android:key="autosave"
android:summary="@string/pref_description_autosave"
android:title="@string/pref_title_autosave" />
<ListPreference
android:key="@string/key_default_view"
android:entries="@array/pref_entries_default_view"
android:entryValues="@array/pref_values_default_view"
android:defaultValue="@string/value_edit_view"
android:title="@string/pref_title_default_view" />
</PreferenceScreen>