1111import com .mozilla .speechlibrary .utils .zip .UnzipCallback ;
1212import com .mozilla .speechlibrary .utils .zip .UnzipTask ;
1313
14+ import org .mozilla .geckoview .GeckoSession ;
1415import org .mozilla .vrbrowser .R ;
1516import org .mozilla .vrbrowser .browser .SettingsStore ;
1617import org .mozilla .vrbrowser .downloads .Download ;
1920import org .mozilla .vrbrowser .ui .widgets .WidgetManagerDelegate ;
2021
2122import java .io .File ;
23+ import java .util .ArrayList ;
2224
2325public class EnvironmentsManager implements DownloadsManager .DownloadsListener , SharedPreferences .OnSharedPreferenceChangeListener {
2426
27+ public interface EnvironmentListener {
28+ default void onEnvironmentSetSuccess (@ NonNull String envId ) {}
29+ default void onEnvironmentSetError (@ NonNull String error ) {}
30+ }
31+
2532 private WidgetManagerDelegate mApplicationDelegate ;
2633 private Context mContext ;
2734 private DownloadsManager mDownloadManager ;
2835 private SharedPreferences mPrefs ;
36+ private ArrayList <EnvironmentListener > mListeners ;
2937
3038 public EnvironmentsManager (@ NonNull Context context ) {
3139 mContext = context ;
3240 mApplicationDelegate = ((WidgetManagerDelegate )context );
3341 mDownloadManager = mApplicationDelegate .getServicesProvider ().getDownloadsManager ();
3442 mPrefs = PreferenceManager .getDefaultSharedPreferences (mContext );
43+ mListeners = new ArrayList <>();
44+ }
45+
46+ public void addListener (@ NonNull EnvironmentListener listener ) {
47+ if (!mListeners .contains (listener )) {
48+ mListeners .add (listener );
49+ }
50+ }
51+
52+ public void removeListener (@ NonNull EnvironmentListener listener ) {
53+ if (mListeners .contains (listener )) {
54+ mListeners .remove (listener );
55+ }
3556 }
3657
3758 public void start () {
@@ -46,11 +67,15 @@ public void stop() {
4667
4768 public void setOrDownloadEnvironment (@ NonNull String envId ) {
4869 if (EnvironmentUtils .isBuiltinEnvironment (mContext , envId )) {
70+ SettingsStore .getInstance (mContext ).setEnvironment (envId );
71+ mListeners .forEach (environmentListener -> environmentListener .onEnvironmentSetSuccess (envId ));
4972 mApplicationDelegate .updateEnvironment ();
5073
5174 } else {
5275 if (EnvironmentUtils .isExternalEnvReady (mContext , envId )) {
5376 // If the environment is ready, call native to update
77+ SettingsStore .getInstance (mContext ).setEnvironment (envId );
78+ mListeners .forEach (environmentListener -> environmentListener .onEnvironmentSetSuccess (envId ));
5479 mApplicationDelegate .updateEnvironment ();
5580
5681 } else {
@@ -98,7 +123,29 @@ private void downloadEnvironment(@NonNull String envId) {
98123 if (!isDownloading ) {
99124 // If the env is not being downloaded, start downloading it
100125 DownloadJob job = DownloadJob .create (environment .getPayload ());
101- mDownloadManager .startDownload (job );
126+ @ SettingsStore .Storage int storage = SettingsStore .getInstance (mContext ).getDownloadsStorage ();
127+ if (storage == SettingsStore .EXTERNAL &&
128+ !mApplicationDelegate .isPermissionGranted (android .Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
129+ mApplicationDelegate .requestPermission (
130+ job .getUri (),
131+ android .Manifest .permission .WRITE_EXTERNAL_STORAGE ,
132+ new GeckoSession .PermissionDelegate .Callback () {
133+ @ Override
134+ public void grant () {
135+ mDownloadManager .startDownload (job );
136+ }
137+
138+ @ Override
139+ public void reject () {
140+ mListeners .forEach (listener -> listener .onEnvironmentSetError (
141+ mContext .getString (R .string .environment_download_permission_error_body )
142+ ));
143+ }
144+ });
145+
146+ } else {
147+ mDownloadManager .startDownload (job );
148+ }
102149 }
103150 }
104151 }
@@ -130,17 +177,23 @@ public void onUnzipFinish(@NonNull String zipFile, @NonNull String outputPath) {
130177 file .delete ();
131178
132179 // the environment is ready, call native to update the current env.
180+ SettingsStore .getInstance (mContext ).setEnvironment (env .getValue ());
181+ mListeners .forEach (environmentListener -> environmentListener .onEnvironmentSetSuccess (env .getValue ()));
133182 mApplicationDelegate .updateEnvironment ();
134183 }
135184
136185 @ Override
137186 public void onUnzipCancelled (@ NonNull String zipFile ) {
138-
187+ mListeners .forEach (listener -> listener .onEnvironmentSetError (
188+ mContext .getString (R .string .environment_download_unzip_error_body )
189+ ));
139190 }
140191
141192 @ Override
142193 public void onUnzipError (@ NonNull String zipFile , @ Nullable String error ) {
143-
194+ mListeners .forEach (listener -> listener .onEnvironmentSetError (
195+ mContext .getString (R .string .environment_download_unzip_error_body )
196+ ));
144197 }
145198 });
146199 String zipOutputPath = EnvironmentUtils .getEnvPath (mContext , env .getValue ());
0 commit comments