2017-11-21 15:35:41 +00:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-06-21 13:59:06 +00:00
|
|
|
import { Component, OnInit, NgZone } from '@angular/core';
|
2018-01-08 13:42:08 +00:00
|
|
|
import { Platform } from 'ionic-angular';
|
2017-10-18 10:20:40 +00:00
|
|
|
import { StatusBar } from '@ionic-native/status-bar';
|
2018-02-23 13:44:17 +00:00
|
|
|
import { CoreAppProvider } from '@providers/app';
|
|
|
|
import { CoreEventsProvider } from '@providers/events';
|
2018-05-17 10:46:24 +00:00
|
|
|
import { CoreLangProvider } from '@providers/lang';
|
2018-02-23 13:44:17 +00:00
|
|
|
import { CoreLoggerProvider } from '@providers/logger';
|
2018-05-17 10:46:24 +00:00
|
|
|
import { CoreSitesProvider } from '@providers/sites';
|
2018-02-23 13:44:17 +00:00
|
|
|
import { CoreLoginHelperProvider } from '@core/login/providers/helper';
|
2017-10-18 10:20:40 +00:00
|
|
|
|
|
|
|
@Component({
|
2017-11-21 15:35:41 +00:00
|
|
|
templateUrl: 'app.html'
|
2017-10-18 10:20:40 +00:00
|
|
|
})
|
2018-01-08 13:42:08 +00:00
|
|
|
export class MoodleMobileApp implements OnInit {
|
2018-01-29 09:05:20 +00:00
|
|
|
// Use page name (string) because the page is lazy loaded (Ionic feature). That way we can load pages without importing them.
|
|
|
|
// The downside is that each page needs to implement a ngModule.
|
|
|
|
rootPage: any = 'CoreLoginInitPage';
|
2017-12-08 14:15:27 +00:00
|
|
|
protected logger;
|
|
|
|
protected lastUrls = {};
|
2017-10-18 10:20:40 +00:00
|
|
|
|
2018-06-15 08:47:44 +00:00
|
|
|
constructor(private platform: Platform, statusBar: StatusBar, logger: CoreLoggerProvider,
|
2018-06-21 13:59:06 +00:00
|
|
|
private eventsProvider: CoreEventsProvider, private loginHelper: CoreLoginHelperProvider, private zone: NgZone,
|
2018-05-17 10:46:24 +00:00
|
|
|
private appProvider: CoreAppProvider, private langProvider: CoreLangProvider, private sitesProvider: CoreSitesProvider) {
|
2017-12-08 14:15:27 +00:00
|
|
|
this.logger = logger.getInstance('AppComponent');
|
|
|
|
|
2017-11-21 15:35:41 +00:00
|
|
|
platform.ready().then(() => {
|
|
|
|
// Okay, so the platform is ready and our plugins are available.
|
|
|
|
// Here you can do any higher level native things you might need.
|
2018-06-13 07:54:52 +00:00
|
|
|
if (platform.is('android')) {
|
|
|
|
statusBar.styleLightContent();
|
|
|
|
} else {
|
|
|
|
statusBar.styleDefault();
|
|
|
|
}
|
2017-11-21 15:35:41 +00:00
|
|
|
});
|
2017-12-08 07:23:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-08 13:42:08 +00:00
|
|
|
* Component being initialized.
|
2017-12-08 07:23:52 +00:00
|
|
|
*/
|
2018-01-29 09:05:20 +00:00
|
|
|
ngOnInit(): void {
|
2017-12-08 07:23:52 +00:00
|
|
|
this.eventsProvider.on(CoreEventsProvider.LOGOUT, () => {
|
2018-05-17 10:46:24 +00:00
|
|
|
// Go to sites page when user is logged out.
|
2018-01-08 13:42:08 +00:00
|
|
|
this.appProvider.getRootNavController().setRoot('CoreLoginSitesPage');
|
2018-05-17 10:46:24 +00:00
|
|
|
|
|
|
|
// Unload lang custom strings.
|
|
|
|
this.langProvider.clearCustomStrings();
|
2017-12-08 07:23:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for session expired events.
|
|
|
|
this.eventsProvider.on(CoreEventsProvider.SESSION_EXPIRED, (data) => {
|
|
|
|
this.loginHelper.sessionExpired(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for passwordchange and usernotfullysetup events to open InAppBrowser.
|
|
|
|
this.eventsProvider.on(CoreEventsProvider.PASSWORD_CHANGE_FORCED, (data) => {
|
|
|
|
this.loginHelper.openInAppForEdit(data.siteId, '/login/change_password.php', 'core.forcepasswordchangenotice', true);
|
|
|
|
});
|
|
|
|
this.eventsProvider.on(CoreEventsProvider.USER_NOT_FULLY_SETUP, (data) => {
|
|
|
|
this.loginHelper.openInAppForEdit(data.siteId, '/user/edit.php', 'core.usernotfullysetup');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for sitepolicynotagreed event to accept the site policy.
|
|
|
|
this.eventsProvider.on(CoreEventsProvider.SITE_POLICY_NOT_AGREED, (data) => {
|
|
|
|
this.loginHelper.sitePolicyNotAgreed(data.siteId);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check URLs loaded in any InAppBrowser.
|
|
|
|
this.eventsProvider.on(CoreEventsProvider.IAB_LOAD_START, (event) => {
|
|
|
|
this.loginHelper.inAppBrowserLoadStart(event.url);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check InAppBrowser closed.
|
|
|
|
this.eventsProvider.on(CoreEventsProvider.IAB_EXIT, () => {
|
|
|
|
this.loginHelper.waitingForBrowser = false;
|
|
|
|
this.loginHelper.lastInAppUrl = '';
|
|
|
|
this.loginHelper.checkLogout();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.platform.resume.subscribe(() => {
|
|
|
|
// Wait a second before setting it to false since in iOS there could be some frozen WS calls.
|
|
|
|
setTimeout(() => {
|
|
|
|
this.loginHelper.waitingForBrowser = false;
|
|
|
|
this.loginHelper.checkLogout();
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
|
2017-12-08 14:15:27 +00:00
|
|
|
// Handle app launched with a certain URL (custom URL scheme).
|
2018-01-29 09:05:20 +00:00
|
|
|
(<any> window).handleOpenURL = (url: string): void => {
|
2018-06-21 13:59:06 +00:00
|
|
|
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
|
|
|
this.zone.run(() => {
|
|
|
|
// First check that the URL hasn't been treated a few seconds ago. Sometimes this function is called more than once.
|
|
|
|
if (this.lastUrls[url] && Date.now() - this.lastUrls[url] < 3000) {
|
|
|
|
// Function called more than once, stop.
|
|
|
|
return;
|
|
|
|
}
|
2017-12-08 14:15:27 +00:00
|
|
|
|
2018-06-21 13:59:06 +00:00
|
|
|
this.logger.debug('App launched by URL ', url);
|
2017-12-08 14:15:27 +00:00
|
|
|
|
2018-06-21 13:59:06 +00:00
|
|
|
this.lastUrls[url] = Date.now();
|
2017-12-08 14:15:27 +00:00
|
|
|
|
2018-06-21 13:59:06 +00:00
|
|
|
this.eventsProvider.trigger(CoreEventsProvider.APP_LAUNCHED_URL, url);
|
|
|
|
});
|
2017-12-08 14:15:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Listen for app launched URLs. If we receive one, check if it's a SSO authentication.
|
|
|
|
this.eventsProvider.on(CoreEventsProvider.APP_LAUNCHED_URL, (url) => {
|
|
|
|
this.loginHelper.appLaunchedByURL(url);
|
|
|
|
});
|
2018-05-17 10:46:24 +00:00
|
|
|
|
|
|
|
// Load custom lang strings. This cannot be done inside the lang provider because it causes circular dependencies.
|
|
|
|
const loadCustomStrings = (): void => {
|
|
|
|
const currentSite = this.sitesProvider.getCurrentSite(),
|
|
|
|
customStrings = currentSite && currentSite.getStoredConfig('tool_mobile_customlangstrings');
|
|
|
|
|
|
|
|
if (typeof customStrings != 'undefined') {
|
|
|
|
this.langProvider.loadCustomStrings(customStrings);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.eventsProvider.on(CoreEventsProvider.LOGIN, () => {
|
|
|
|
loadCustomStrings();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.eventsProvider.on(CoreEventsProvider.SITE_UPDATED, (siteId) => {
|
|
|
|
if (siteId == this.sitesProvider.getCurrentSiteId()) {
|
|
|
|
loadCustomStrings();
|
|
|
|
}
|
|
|
|
});
|
2018-07-11 10:16:17 +00:00
|
|
|
|
|
|
|
// Pause Youtube videos in Android when app is put in background or screen is locked.
|
|
|
|
this.platform.pause.subscribe(() => {
|
|
|
|
if (!this.platform.is('android')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pauseVideos = (window: Window): void => {
|
|
|
|
// Search videos in iframes recursively.
|
|
|
|
for (let i = 0; i < window.length; i++) {
|
|
|
|
pauseVideos(window[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window.location.hostname.match(/^www\.youtube(-nocookie)?\.com$/)) {
|
|
|
|
// Embedded Youtube video, pause it.
|
|
|
|
const videos = window.document.querySelectorAll('video');
|
|
|
|
for (let i = 0; i < videos.length; i++) {
|
|
|
|
videos[i].pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pauseVideos(window);
|
|
|
|
});
|
2017-11-21 15:35:41 +00:00
|
|
|
}
|
2017-10-18 10:20:40 +00:00
|
|
|
}
|