use of ca.etsmtl.applets.etsmobile.util.SecurePreferences in project ETSMobile-Android2 by ApplETS.
the class ETSGcmListenerService method sendNotification.
/**
* Create and show a simple notification containing the received GCM message.
*
* @param data GCM message received.
*/
private void sendNotification(Bundle data) {
SecurePreferences securePreferences = new SecurePreferences(this);
Gson gson = new Gson();
String receivedNotifString = securePreferences.getString(Constants.RECEIVED_NOTIF, "");
ArrayList<MonETSNotification> receivedNotif = gson.fromJson(receivedNotifString, new TypeToken<ArrayList<MonETSNotification>>() {
}.getType());
if (receivedNotif == null) {
receivedNotif = new ArrayList<>();
}
MonETSNotification nouvelleNotification = getMonETSNotificationFromBundle(data);
receivedNotif.add(nouvelleNotification);
int numberOfNotifications = receivedNotif.size();
Intent intent = new Intent(this, NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_ets);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.school_48).setColor(getResources().getColor(R.color.red)).setContentTitle(getString(R.string.ets)).setContentText(getString(R.string.new_notifications)).setContentIntent(pendingIntent).setLargeIcon(icon).setAutoCancel(true).setNumber(numberOfNotifications);
NotificationCompat.InboxStyle inBoxStyle = new NotificationCompat.InboxStyle();
// Sets a title for the Inbox in expanded layout
String bigContentTitle = getString(R.string.notification_content_title, numberOfNotifications + "", (numberOfNotifications == 1 ? "" : "s"), (numberOfNotifications == 1 ? "" : "s"));
inBoxStyle.setBigContentTitle(bigContentTitle);
String username = ApplicationManager.userCredentials.getUsername();
Spannable sb = new SpannableString(username);
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, username.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inBoxStyle.setSummaryText(sb);
securePreferences.edit().putString(Constants.RECEIVED_NOTIF, gson.toJson(receivedNotif)).commit();
int minimumIndex = receivedNotif.size() - NUMBER_OF_NOTIF_TO_DISPLAY;
minimumIndex = minimumIndex < 0 ? 0 : minimumIndex;
for (int i = receivedNotif.size() - 1; i >= minimumIndex; i--) {
inBoxStyle.addLine(receivedNotif.get(i).getNotificationTexte());
}
if (numberOfNotifications > NUMBER_OF_NOTIF_TO_DISPLAY) {
int plusOthers = (numberOfNotifications - NUMBER_OF_NOTIF_TO_DISPLAY);
String plusOthersString = getString(R.string.others_notifications, plusOthers + "", (plusOthers == 1 ? "" : "s"));
Spannable others = new SpannableString(plusOthersString);
others.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, others.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inBoxStyle.addLine(others);
}
mBuilder.setStyle(inBoxStyle);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
use of ca.etsmtl.applets.etsmobile.util.SecurePreferences in project ETSMobile-Android2 by ApplETS.
the class AuthentificationPortailTask method onPostExecute.
protected void onPostExecute(Intent intent) {
if (intent != null) {
Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
if (accounts.length > 0) {
String authtoken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
if (!TextUtils.isEmpty(authtoken)) {
int typeUsagerId = intent.getIntExtra(Constants.TYPE_USAGER_ID, -1);
String domaine = intent.getStringExtra(Constants.DOMAINE);
SecurePreferences securePreferences = new SecurePreferences(launchingActivity);
securePreferences.edit().putInt(Constants.TYPE_USAGER_ID, typeUsagerId).commit();
securePreferences.edit().putString(Constants.DOMAINE, domaine).commit();
securePreferences.edit().putString(Constants.EXP_DATE_COOKIE, domaine).commit();
ApplicationManager.domaine = domaine;
ApplicationManager.typeUsagerId = typeUsagerId;
accountManager.setAuthToken(accounts[0], Constants.AUTH_TOKEN_TYPE, authtoken);
Utility.saveCookieExpirationDate(authtoken, securePreferences);
Intent gcmRegistrationIntent = new Intent(launchingActivity, RegistrationIntentService.class);
launchingActivity.startService(gcmRegistrationIntent);
}
}
launchingActivity.finish();
}
}
use of ca.etsmtl.applets.etsmobile.util.SecurePreferences in project ETSMobile-Android2 by ApplETS.
the class ApplicationManager method onCreate.
@Override
public void onCreate() {
super.onCreate();
AnalyticsHelper.getInstance(this);
createDatabaseTables();
// SupportKit.init(this, getString(R.string.credentials_supportkit));
// Fabric.with(this, new Crashlytics());
AccountManager accountManager = AccountManager.get(this);
Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
String username = "", password = "";
if (accounts.length > 0) {
username = accounts[0].name;
password = accountManager.getPassword(accounts[0]);
}
if (username.length() > 0 && password.length() > 0) {
userCredentials = new UserCredentials(username, password);
}
SecurePreferences securePreferences = new SecurePreferences(this);
int typeUsagerId = securePreferences.getInt(Constants.TYPE_USAGER_ID, -1);
String domaine = securePreferences.getString(Constants.DOMAINE, "");
if (typeUsagerId != -1 && !TextUtils.isEmpty(domaine)) {
ApplicationManager.typeUsagerId = typeUsagerId;
ApplicationManager.domaine = domaine;
}
}
use of ca.etsmtl.applets.etsmobile.util.SecurePreferences in project ETSMobile-Android2 by ApplETS.
the class NotificationActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications);
securePreferences = new SecurePreferences(this);
progressBar = (ProgressBar) findViewById(R.id.progressBar_notifications);
listView = (ListView) findViewById(R.id.listView_notifications);
progressBar.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.ets_red_fonce), PorterDuff.Mode.MULTIPLY);
progressBar.setVisibility(View.VISIBLE);
notificationsAdapter = new NotificationsAdapter(this, R.layout.row_notification, new ArrayList<MonETSNotification>());
listView.setAdapter(notificationsAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MonETSNotification item = notificationsAdapter.getItem(position);
String url = item.getUrl();
if (URLUtil.isValidUrl(url)) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
}
});
syncAdapterWithDB();
Utility.loadNotifications(this, this);
}
use of ca.etsmtl.applets.etsmobile.util.SecurePreferences in project ETSMobile-Android2 by ApplETS.
the class MoodleFragment method onRequestSuccess.
@Override
public void onRequestSuccess(Object o) {
try {
if (o instanceof MoodleToken) {
MoodleToken moodleToken = (MoodleToken) o;
SecurePreferences securePreferences = new SecurePreferences(getActivity());
securePreferences.edit().putString(UserCredentials.MOODLE_TOKEN, moodleToken.getToken()).commit();
ApplicationManager.userCredentials.setMoodleToken(moodleToken.getToken());
if (moodleToken.getToken().equals("")) {
throw new Exception("Impossible de se connecter");
}
queryMoodleProfile(moodleToken);
}
if (o instanceof MoodleProfile) {
MoodleProfile moodleProfile = (MoodleProfile) o;
queryMoodleCourses(moodleProfile);
}
if (o instanceof MoodleCourses) {
MoodleCourses moodleCourses = (MoodleCourses) o;
moodleCoursesAdapter = new MoodleCoursesAdapter(getActivity(), R.layout.row_moodle_course, this);
Collections.sort(moodleCourses, new CourseComparator());
// To get the most current semester first
Collections.reverse(moodleCourses);
String semesterString;
List<String> semesterList = new ArrayList<>();
for (MoodleCourse moodleCourse : moodleCourses) {
if (moodleCourse.getFullname().matches("(.*)([AÉH](\\d){4})(.*)"))
semesterString = moodleCourse.getFullname().replace("(", "{").split("\\{")[1].replace(")", "");
else
semesterString = null;
semesterString = convertSemesterString(semesterString);
if (!semesterList.contains(semesterString)) {
semesterList.add(semesterString);
MoodleCourse courseSemesterSeparator = new MoodleCourse();
courseSemesterSeparator.setCourseSemester(semesterString);
moodleCoursesAdapter.addSectionHeader(courseSemesterSeparator);
moodleCoursesAdapter.addCourse(moodleCourse);
} else
moodleCoursesAdapter.addCourse(moodleCourse);
}
moodleCoursesListView.setAdapter(moodleCoursesAdapter);
moodleCoursesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MoodleCourse moodleCourse = (MoodleCourse) parent.getItemAtPosition(position);
if (moodleCourse.getId() != MoodleCourse.IS_SEMESTER) {
Intent i = new Intent(getActivity(), MoodleCourseActivity.class);
i.putExtra("idCours", moodleCourse.getId());
i.putExtra("nameCours", moodleCourse.getShortname());
getActivity().startActivity(i);
}
}
});
super.onRequestSuccess(null);
}
} catch (Exception e) {
Log.w("MoodleFragment", "Exception caught in onRequestSuccess: " + e);
if (getActivity() != null)
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Aggregations