Added ability to load files

This commit is contained in:
William Brawner 2017-07-27 18:35:23 -05:00
parent 74a6f930a5
commit b1fb0dcea0
4 changed files with 53 additions and 7 deletions

View file

@ -15,6 +15,7 @@ import android.support.v7.app.AppCompatActivity;
public class FileUtils {
public static final int WRITE_PERMISSION_REQUEST = 0;
public static final int OPEN_FILE_REQUEST = 1;
private Activity mContext;

View file

@ -44,8 +44,10 @@ implements ActivityCompat.OnRequestPermissionsResultCallback {
public static final String AUTHORITY = "com.wbrawner.simplemarkdown.fileprovider";
private static final int REQUEST_WRITE_STORAGE = 0;
private static File mFilesDir;
@BindView(R.id.pager) ViewPager pager;
@BindView(R.id.layout_tab) TabLayout tabLayout;
@BindView(R.id.pager)
ViewPager pager;
@BindView(R.id.layout_tab)
TabLayout tabLayout;
private static final String TAG = MainActivity.class.getSimpleName();
private static String fileName;
@ -178,6 +180,9 @@ implements ActivityCompat.OnRequestPermissionsResultCallback {
}
}
break;
case R.id.action_load:
requestOpen();
break;
}
return super.onOptionsItemSelected(item);
}
@ -189,6 +194,24 @@ implements ActivityCompat.OnRequestPermissionsResultCallback {
.sendBroadcast(saveIntent);
}
private void requestOpen() {
Intent openIntent = new Intent(Intent.ACTION_GET_CONTENT);
openIntent.setType("*/*");
openIntent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(
openIntent,
getString(R.string.open_file)
),
FileUtils.OPEN_FILE_REQUEST
);
} catch (ActivityNotFoundException e) {
Toast.makeText(MainActivity.this, R.string.no_filebrowser, Toast.LENGTH_SHORT)
.show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
@ -256,4 +279,18 @@ implements ActivityCompat.OnRequestPermissionsResultCallback {
else
pager.setCurrentItem(EditPagerAdapter.FRAGMENT_EDIT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FileUtils.OPEN_FILE_REQUEST:
if (resultCode == RESULT_OK) {
Uri fileUri = data.getData();
Intent loadIntent = new Intent(EditFragment.LOAD_ACTION);
loadIntent.putExtra("fileUri", fileUri.toString());
LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(loadIntent);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}

View file

@ -11,6 +11,11 @@
android:id="@+id/action_share"
android:icon="@android:drawable/ic_menu_share"
app:showAsAction="always" />
<item
android:id="@+id/action_load"
android:orderInCategory="100"
android:title="@string/action_load"
app:showAsAction="never" />
<!--<item-->
<!--android:id="@+id/action_help"-->
<!--android:orderInCategory="100"-->

View file

@ -16,4 +16,7 @@
<string name="share_file">Share file to...</string>
<string name="no_permissions">Unable to save file without permissions</string>
<string name="file_saved">File saved to %1$s</string>
<string name="action_load">Load</string>
<string name="open_file">Select a file to open</string>
<string name="no_filebrowser">No file browser apps found</string>
</resources>