summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/wallmotion/SettingsActivity.java
blob: 5fee7df0d74cc7aad09119df5ca983d64bf446d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.javispedro.wallmotion;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.OpenableColumns;
import android.text.TextUtils;
import android.util.Log;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

public class SettingsActivity extends AppCompatActivity {
    private static final String TAG = "SettingsActivity";

    public static final String ACTION_CHOOSE_FILE = "com.javispedro.wallmotion.choose_file";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);

        SettingsFragment fragment = new SettingsFragment();
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.settings, fragment)
                .commit();

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        private static final String TAG = "SettingsFragment";

        private final static int PICK_VIDEO_FILE = 1;

        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
            Preference video_file = findPreference(getString(R.string.settings_video_file_key));
            video_file.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    openVideoFilePicker();
                    return true;
                }
            });
            video_file.setSummary(getVideoFileSummary());
            Preference video_file_clear = findPreference(getString(R.string.settings_video_file_clear_key));
            video_file_clear.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    setVideoFile(null);
                    return true;
                }
            });
        }

        @Override
        public void onStart() {
            super.onStart();
            Intent launchIntent = getActivity().getIntent();
            if (launchIntent != null && launchIntent.getAction().equals(ACTION_CHOOSE_FILE)) {
                openVideoFilePicker();
                getActivity().finish();
            }
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode,
                                     Intent resultData) {
            switch (requestCode) {
                case PICK_VIDEO_FILE:
                    if (resultCode == Activity.RESULT_OK && resultData != null) {
                        Uri uri = resultData.getData();
                        setVideoFile(uri);
                    }
                    break;
            }
        }

        private void openVideoFilePicker() {
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("video/*");
            intent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
            startActivityForResult(intent, PICK_VIDEO_FILE);
        }

        private void setVideoFile(Uri uri) {
            ContentResolver resolver = getContext().getContentResolver();

            // First, release any permission request on the current uri
            String cur_value = getStringPref(getString(R.string.settings_video_file_key));
            if (!TextUtils.isEmpty(cur_value)) {
                Uri cur_uri = Uri.parse(cur_value);

                if (cur_uri.equals(uri)) {
                    Log.d(TAG, "identical uri selected");
                    return;
                }

                Log.d(TAG, "release persistable uri permission on uri: " + cur_uri.toString());
                try {
                    resolver.releasePersistableUriPermission(cur_uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
                } catch (Exception ex) {
                    Log.w(TAG, "could not release persistable uri permission on uri: " + cur_uri.toString());
                    ex.printStackTrace();
                }
            }

            // Then, store the new setting
            if (uri != null) {
                Log.d(TAG, "storing video_file pref uri: " + uri.toString());
                setStringPref(getString(R.string.settings_video_file_key), uri.toString());

                // Take a persistent permission on it
                Log.d(TAG, "take persistable uri permission on uri: " + uri.toString());
                resolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                Log.d(TAG, "clearing video_file pref uri");
                clearPref(getString(R.string.settings_video_file_key));
            }

            // Refresh the UI summary
            Preference video_file = findPreference(getString(R.string.settings_video_file_key));
            video_file.setSummary(getVideoFileSummary());
        }

        private String getVideoFileSummary() {
            String cur_value = getStringPref(getString(R.string.settings_video_file_key));
            if (!TextUtils.isEmpty(cur_value)) {
                ContentResolver resolver = getContext().getContentResolver();
                Uri uri = Uri.parse(cur_value);
                String[] projection = {OpenableColumns.DISPLAY_NAME};
                try {
                    Cursor cursor = resolver.query(uri, projection, null, null, null);
                    if (cursor != null) {
                        int col = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                        if (col >= 0 && cursor.moveToFirst()) {
                            String name = cursor.getString(col);
                            cursor.close();
                            Log.d(TAG, "got video file display_name: " + name);
                            return name;
                        } else {
                            Log.w(TAG, "Could not navigate cursor for URI: " + uri.toString());
                        }
                        cursor.close();
                    } else {
                        Log.w(TAG, "Could not get cursor for URI: " + uri.toString());
                    }
                } catch (java.lang.SecurityException ex) {
                    ex.printStackTrace();
                    Log.w(TAG, "Security exception while reading URI: " + uri.toString());
                }
                return uri.getLastPathSegment();
            }
            return getString(R.string.settings_not_set);
        }

        protected String getStringPref(String pref) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
            return prefs.getString(pref, null);
        }

        protected void setStringPref(String pref, String new_value) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
            String cur_value = prefs.getString(pref, null);
            if (!TextUtils.equals(cur_value, new_value)) {
                SharedPreferences.Editor editor = prefs.edit();
                editor.putString(pref, new_value);
                editor.apply();
            }
        }

        protected void clearPref(String pref) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
            if (prefs.contains(pref)) {
                SharedPreferences.Editor editor = prefs.edit();
                editor.remove(pref);
                editor.apply();
            }
        }
    }
}