Search in sources :

Example 1 with LentItem

use of de.geeksfactory.opacclient.objects.LentItem in project opacclient by opacapp.

the class MainActivity method onCreate.

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sp = PreferenceManager.getDefaultSharedPreferences(this);
    String itemToSelect = null;
    if (getIntent() != null && getIntent().getAction() != null) {
        if (getIntent().getAction().equals("android.intent.action.VIEW")) {
            urlintent();
        } else if (getIntent().getAction().equals(ACTION_SEARCH) || getIntent().getAction().equals(ACTION_ACCOUNT)) {
            String lib = getIntent().getStringExtra("library");
            AccountDataSource adata = new AccountDataSource(this);
            List<Account> accounts = adata.getAllAccounts(lib);
            if (accounts.size() == 0) {
                // Check if library exists (an IOException should be thrown otherwise, correct?)
                try {
                    app.getLibrary(lib);
                } catch (IOException | JSONException e) {
                    return;
                }
                Account account = new Account();
                account.setLibrary(lib);
                account.setLabel(getString(R.string.default_account_name));
                account.setName("");
                account.setPassword("");
                long id = adata.addAccount(account);
                selectaccount(id);
            } else if (accounts.size() == 1) {
                selectaccount(accounts.get(0).getId());
            } else if (accounts.size() > 0) {
                if (getIntent().getAction().equals(ACTION_ACCOUNT)) {
                    List<Account> accountsWithPassword = new ArrayList<>();
                    for (Account account : accounts) {
                        if (account.getName() != null && account.getPassword() != null && !account.getName().isEmpty() && !account.getPassword().isEmpty()) {
                            accountsWithPassword.add(account);
                        }
                    }
                    if (accountsWithPassword.size() == 1) {
                        selectaccount(accountsWithPassword.get(0).getId());
                    } else {
                        showAccountSelectDialog(accounts);
                    }
                } else {
                    showAccountSelectDialog(accounts);
                }
            }
            if (getIntent().getAction().equals(ACTION_SEARCH)) {
                itemToSelect = "search";
            } else {
                itemToSelect = "account";
            }
        }
    }
    if (savedInstanceState == null) {
        if (getIntent().hasExtra(EXTRA_FRAGMENT)) {
            selectItem(getIntent().getStringExtra(EXTRA_FRAGMENT));
        } else if (getIntent().hasExtra(ReminderBroadcastReceiver.EXTRA_ALARM_ID)) {
            AccountDataSource adata = new AccountDataSource(this);
            long alid = getIntent().getLongExtra(ReminderBroadcastReceiver.EXTRA_ALARM_ID, -1);
            Alarm alarm = adata.getAlarm(alid);
            if (alarm == null) {
                throw new DataIntegrityException("Unknown alarm ID " + alid + " received.");
            }
            List<LentItem> items = adata.getLentItems(alarm.media);
            if (items.size() > 0) {
                long firstAccount = items.get(0).getAccount();
                boolean multipleAccounts = false;
                for (LentItem item : items) {
                    if (item.getAccount() != firstAccount) {
                        multipleAccounts = true;
                        break;
                    }
                }
                if (multipleAccounts) {
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            drawerLayout.openDrawer(drawer);
                        }
                    }, 500);
                } else {
                    selectaccount(firstAccount);
                    selectItem("account");
                }
            }
        } else if (itemToSelect != null) {
            selectItem(itemToSelect);
        } else if (sp.contains("startup_fragment")) {
            selectItem(sp.getString("startup_fragment", "search"));
        } else {
            selectItem(0);
        }
    }
    try {
        if (nfc_capable) {
            if (!getPackageManager().hasSystemFeature("android.hardware.nfc")) {
                nfc_capable = false;
            }
        }
        if (nfc_capable) {
            mAdapter = android.nfc.NfcAdapter.getDefaultAdapter(this);
            nfcIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
            IntentFilter ndef = new IntentFilter(android.nfc.NfcAdapter.ACTION_TECH_DISCOVERED);
            try {
                ndef.addDataType("*/*");
            } catch (MalformedMimeTypeException e) {
                throw new RuntimeException("fail", e);
            }
            intentFiltersArray = new IntentFilter[] { ndef };
            techListsArray = new String[][] { new String[] { android.nfc.tech.NfcV.class.getName() } };
        }
    } catch (SecurityException e) {
        e.printStackTrace();
    }
    if (app.getLibrary() != null) {
        getSupportActionBar().setSubtitle(app.getLibrary().getDisplayName());
    }
    showUpdateInfoDialog();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setExitTransition(null);
    }
}
Also used : Account(de.geeksfactory.opacclient.objects.Account) IntentFilter(android.content.IntentFilter) AccountDataSource(de.geeksfactory.opacclient.storage.AccountDataSource) MalformedMimeTypeException(android.content.IntentFilter.MalformedMimeTypeException) DataIntegrityException(de.geeksfactory.opacclient.storage.DataIntegrityException) Handler(android.os.Handler) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Alarm(de.geeksfactory.opacclient.reminder.Alarm) ArrayList(java.util.ArrayList) List(java.util.List) LentItem(de.geeksfactory.opacclient.objects.LentItem) SuppressLint(android.annotation.SuppressLint)

Example 2 with LentItem

use of de.geeksfactory.opacclient.objects.LentItem in project opacclient by opacapp.

the class AccountItemDetailActivity method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    Intent intent = new Intent();
    int resultCode;
    int item_id = menuItem.getItemId();
    if (item_id == R.id.action_prolong) {
        resultCode = RESULT_PROLONG;
        intent.putExtra(EXTRA_DATA, ((LentItem) item).getProlongData());
    } else if (item_id == R.id.action_download) {
        resultCode = RESULT_DOWNLOAD;
        intent.putExtra(EXTRA_DATA, ((LentItem) item).getDownloadData());
    } else if (item_id == R.id.action_cancel) {
        resultCode = RESULT_CANCEL;
        intent.putExtra(EXTRA_DATA, ((ReservedItem) item).getCancelData());
    } else if (item_id == R.id.action_booking) {
        resultCode = RESULT_BOOKING;
        intent.putExtra(EXTRA_DATA, ((ReservedItem) item).getBookingData());
    } else {
        return super.onOptionsItemSelected(menuItem);
    }
    setResult(resultCode, intent);
    supportFinishAfterTransition();
    return true;
}
Also used : Intent(android.content.Intent) ReservedItem(de.geeksfactory.opacclient.objects.ReservedItem) LentItem(de.geeksfactory.opacclient.objects.LentItem)

Example 3 with LentItem

use of de.geeksfactory.opacclient.objects.LentItem in project opacclient by opacapp.

the class TouchPointAccountTest method testParseMediaList.

@Test
public void testParseMediaList() throws OpacApi.OpacErrorException {
    String html = readResource("/touchpoint/medialist/" + file);
    // we may not have all files for all libraries
    if (html == null)
        return;
    List<LentItem> media = TouchPoint.parse_medialist(Jsoup.parse(html));
    assertTrue(media.size() > 0);
    for (LentItem item : media) {
        assertNotNull(item.getTitle());
        assertNotNull(item.getDeadline());
        if (item.getStatus().contains("ist möglich"))
            assertNotNull(item.getProlongData());
    }
}
Also used : LentItem(de.geeksfactory.opacclient.objects.LentItem) Test(org.junit.Test)

Example 4 with LentItem

use of de.geeksfactory.opacclient.objects.LentItem in project opacclient by opacapp.

the class ReminderBroadcastReceiver method showNotification.

private void showNotification() {
    setUpChannel();
    if (!prefs.getBoolean("notification_service", false)) {
        if (BuildConfig.DEBUG)
            Log.i(LOG_TAG, "not showing notification because disabled");
        return;
    }
    if (alarm.notified) {
        if (BuildConfig.DEBUG) {
            Log.i(LOG_TAG, "not showing notification because already notified");
        }
        return;
    }
    if (BuildConfig.DEBUG)
        Log.i(LOG_TAG, "showing notification");
    List<LentItem> expiringItems = new ArrayList<>();
    List<Long> updatedItemIds = new ArrayList<>();
    for (long mediaId : alarm.media) {
        LentItem item = adata.getLentItem(mediaId);
        if (item == null) {
            if (BuildConfig.DEBUG) {
                throw new DataIntegrityException("Unknown media ID " + mediaId + " in alarm with deadline " + alarm.deadline.toString());
            }
        } else {
            expiringItems.add(item);
            updatedItemIds.add(mediaId);
        }
    }
    if (expiringItems.size() == 0) {
        adata.removeAlarm(alarm);
        return;
    }
    alarm.media = listToLongArray(updatedItemIds);
    String notificationText;
    String notificationTitle;
    // expired before the next day
    if (alarm.deadline.isBefore(LocalDate.now())) {
        notificationText = context.getString(R.string.notif_ticker_expired, expiringItems.size());
        notificationTitle = context.getString(R.string.notif_title_expired);
    } else {
        notificationText = context.getString(R.string.notif_ticker, expiringItems.size());
        notificationTitle = context.getString(R.string.notif_title);
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_REMINDER).setContentTitle(notificationTitle).setContentText(notificationText).setTicker(notificationText);
    // Display list of items in notification
    NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
    for (int i = 0; i < 5 && i < expiringItems.size(); i++) {
        style.addLine(expiringItems.get(i).getTitle());
    }
    if (expiringItems.size() > 5) {
        style.setSummaryText(context.getString(R.string.notif_plus_more, expiringItems.size() - 5));
    }
    style.setBigContentTitle(notificationText);
    builder.setStyle(style).setSmallIcon(R.drawable.ic_stat_notification).setWhen(alarm.deadline.toDateTimeAtStartOfDay().getMillis()).setNumber(expiringItems.size()).setColor(context.getResources().getColor(R.color.primary_red)).setSound(null).setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
    // Intent for when notification is deleted
    Intent deleteIntent = new Intent(context, ReminderBroadcastReceiver.class);
    deleteIntent.setAction(ReminderBroadcastReceiver.ACTION_NOTIFICATION_DELETED);
    deleteIntent.putExtra(ReminderBroadcastReceiver.EXTRA_ALARM_ID, alarm.id);
    PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context, (int) alarm.id, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setDeleteIntent(deletePendingIntent);
    // Intent for snooze button
    Intent snoozeIntent = new Intent(context, ReminderBroadcastReceiver.class);
    snoozeIntent.setAction(ReminderBroadcastReceiver.ACTION_NOTIFICATION_SNOOZE);
    snoozeIntent.putExtra(ReminderBroadcastReceiver.EXTRA_ALARM_ID, alarm.id);
    PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(context, (int) alarm.id, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.ic_action_alarms, context.getString(R.string.notif_snooze), snoozePendingIntent);
    // Intent for "don't remind again" button
    Intent notAgainIntent = new Intent(context, ReminderBroadcastReceiver.class);
    notAgainIntent.setAction(ReminderBroadcastReceiver.ACTION_NOTIFICATION_DONT_REMIND_AGAIN);
    notAgainIntent.putExtra(ReminderBroadcastReceiver.EXTRA_ALARM_ID, alarm.id);
    PendingIntent notAgainPendingIntent = PendingIntent.getBroadcast(context, (int) alarm.id, notAgainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.ic_action_delete, context.getString(R.string.notif_dont_remind_again), notAgainPendingIntent);
    // Intent for when notification is clicked
    Intent clickIntent = new Intent(context, ReminderBroadcastReceiver.class);
    clickIntent.setAction(ReminderBroadcastReceiver.ACTION_NOTIFICATION_CLICK);
    clickIntent.putExtra(ReminderBroadcastReceiver.EXTRA_ALARM_ID, alarm.id);
    PendingIntent clickPendingIntent = PendingIntent.getBroadcast(context, (int) alarm.id, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(clickPendingIntent);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify((int) alarm.id, builder.build());
    alarm.notified = true;
    adata.updateAlarm(alarm);
}
Also used : NotificationManager(android.app.NotificationManager) DataIntegrityException(de.geeksfactory.opacclient.storage.DataIntegrityException) ArrayList(java.util.ArrayList) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) NotificationCompat(android.support.v4.app.NotificationCompat) LentItem(de.geeksfactory.opacclient.objects.LentItem) PendingIntent(android.app.PendingIntent)

Example 5 with LentItem

use of de.geeksfactory.opacclient.objects.LentItem in project opacclient by opacapp.

the class ReminderHelper method generateAlarms.

private void generateAlarms(int warning, Boolean enabled) {
    // resets the notified field to false for all alarms with finished == false this will re-show
    // notifications that were not dismissed yet (for example after reboot)
    data.resetNotifiedOnAllAlarams();
    if (warning == -1)
        warning = Integer.parseInt(sp.getString("notification_warning", "3"));
    if (warning > 10) {
        // updated from the old app version -> change value to get days instead of milliseconds
        warning = warning / (24 * 60 * 60 * 1000);
        sp.edit().putString("notification_warning", String.valueOf(warning)).apply();
    }
    if (enabled == null)
        enabled = sp.getBoolean("notification_service", false);
    if (!enabled) {
        if (log) {
            Log.d("OpacClient", "scheduling no alarms because notifications are disabled");
        }
        return;
    }
    List<LentItem> items = data.getAllLentItems();
    // Sort lent items by deadline
    Map<LocalDate, List<Long>> arrangedIds = new HashMap<>();
    for (LentItem item : items) {
        LocalDate deadline = item.getDeadline();
        if (deadline == null) {
            // this case.
            continue;
        }
        if (item.getDownloadData() != null && item.getDownloadData().contains(":")) {
            // Don't remind people of bringing back ebooks, because ... uhm...
            continue;
        }
        if (!arrangedIds.containsKey(deadline)) {
            arrangedIds.put(deadline, new ArrayList<Long>());
        }
        arrangedIds.get(deadline).add(item.getDbId());
    }
    for (Alarm alarm : data.getAllAlarms()) {
        // Remove alarms with no corresponding media
        if (!arrangedIds.containsKey(alarm.deadline)) {
            cancelNotification(alarm);
            data.removeAlarm(alarm);
        }
    }
    // Find and add/update corresponding alarms for current lent media
    for (Map.Entry<LocalDate, List<Long>> entry : arrangedIds.entrySet()) {
        LocalDate deadline = entry.getKey();
        long[] media = toArray(entry.getValue());
        Alarm alarm = data.getAlarmByDeadline(deadline);
        if (alarm != null) {
            if (!Arrays.equals(media, alarm.media)) {
                alarm.media = media;
                data.updateAlarm(alarm);
            }
        } else {
            if (log) {
                Log.i("OpacClient", "scheduling alarm for " + media.length + " items with deadline on " + DateTimeFormat.shortDate().print(deadline) + " on " + DateTimeFormat.shortDate().print(deadline.minusDays(warning)));
            }
            data.addAlarm(deadline, media, deadline.minusDays(warning).toDateTimeAtStartOfDay());
        }
    }
    scheduleAlarms(true);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) LentItem(de.geeksfactory.opacclient.objects.LentItem) LocalDate(org.joda.time.LocalDate) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

LentItem (de.geeksfactory.opacclient.objects.LentItem)43 ArrayList (java.util.ArrayList)26 ReservedItem (de.geeksfactory.opacclient.objects.ReservedItem)16 Element (org.jsoup.nodes.Element)16 AccountData (de.geeksfactory.opacclient.objects.AccountData)14 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)12 Test (org.junit.Test)10 JSONObject (org.json.JSONObject)9 Document (org.jsoup.nodes.Document)8 Elements (org.jsoup.select.Elements)8 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)7 Pattern (java.util.regex.Pattern)5 Intent (android.content.Intent)4 Cursor (android.database.Cursor)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Matcher (java.util.regex.Matcher)4 LocalDate (org.joda.time.LocalDate)4 JSONException (org.json.JSONException)4 Account (de.geeksfactory.opacclient.objects.Account)3