Search in sources :

Example 1 with SecurePreferences

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());
}
Also used : MonETSNotification(ca.etsmtl.applets.etsmobile.model.MonETSNotification) NotificationManager(android.app.NotificationManager) Gson(com.google.gson.Gson) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences) SpannableString(android.text.SpannableString) SpannableString(android.text.SpannableString) Bitmap(android.graphics.Bitmap) TypeToken(com.google.gson.reflect.TypeToken) StyleSpan(android.text.style.StyleSpan) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Spannable(android.text.Spannable)

Example 2 with SecurePreferences

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();
    }
}
Also used : Account(android.accounts.Account) Intent(android.content.Intent) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences)

Example 3 with SecurePreferences

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;
    }
}
Also used : Account(android.accounts.Account) AccountManager(android.accounts.AccountManager) UserCredentials(ca.etsmtl.applets.etsmobile.model.UserCredentials) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences)

Example 4 with SecurePreferences

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);
}
Also used : MonETSNotification(ca.etsmtl.applets.etsmobile.model.MonETSNotification) ArrayList(java.util.ArrayList) Intent(android.content.Intent) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) NotificationsAdapter(ca.etsmtl.applets.etsmobile.ui.adapter.NotificationsAdapter) AdapterView(android.widget.AdapterView)

Example 5 with SecurePreferences

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();
    }
}
Also used : ArrayList(java.util.ArrayList) Intent(android.content.Intent) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences) MoodleCourse(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourse) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) SpiceException(com.octo.android.robospice.persistence.exception.SpiceException) MoodleCourseActivity(ca.etsmtl.applets.etsmobile.ui.activity.MoodleCourseActivity) MoodleCoursesAdapter(ca.etsmtl.applets.etsmobile.ui.adapter.MoodleCoursesAdapter) CourseComparator(ca.etsmtl.applets.etsmobile.util.CourseComparator) MoodleToken(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleToken) MoodleCourses(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourses) AdapterView(android.widget.AdapterView) MoodleProfile(ca.etsmtl.applets.etsmobile.model.Moodle.MoodleProfile)

Aggregations

SecurePreferences (ca.etsmtl.applets.etsmobile.util.SecurePreferences)5 Intent (android.content.Intent)4 Account (android.accounts.Account)2 View (android.view.View)2 AdapterView (android.widget.AdapterView)2 ListView (android.widget.ListView)2 MonETSNotification (ca.etsmtl.applets.etsmobile.model.MonETSNotification)2 ArrayList (java.util.ArrayList)2 AccountManager (android.accounts.AccountManager)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Bitmap (android.graphics.Bitmap)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 Spannable (android.text.Spannable)1 SpannableString (android.text.SpannableString)1 StyleSpan (android.text.style.StyleSpan)1 MoodleCourse (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourse)1 MoodleCourses (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCourses)1 MoodleProfile (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleProfile)1 MoodleToken (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleToken)1