Search in sources :

Example 1 with SdCardStorageAdapter

use of com.newsrob.storage.SdCardStorageAdapter in project newsrob by marianokamp.

the class Feed method restoreFeedsIfNeccesary.

public static final boolean restoreFeedsIfNeccesary(Context context) {
    // initial startup?
    final EntryManager em = EntryManager.getInstance(context);
    if (em.getFeedCount() != 0)
        return false;
    SdCardStorageAdapter storageAdapter = new SdCardStorageAdapter(context.getApplicationContext(), false);
    final String fileName = storageAdapter.getAbsolutePathForAsset(FEED_SETTINGS_FILE_NAME);
    if (!new File(fileName).exists()) {
        Log.w("Feed", "No " + fileName + " existing. Not trying to restore feeds.");
        return false;
    }
    Log.i("Feed", "Trying to restore feeds.");
    try {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser parser = saxParserFactory.newSAXParser();
        DefaultHandler handler = new SimpleStringExtractorHandler() {

            @Override
            public final void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
                super.startElement(uri, localName, name, attributes);
                if (!"feed".equals(localName))
                    return;
                Feed f = new Feed();
                f.setAtomId(URLDecoder.decode(attributes.getValue("atomId")));
                f.setTitle(URLDecoder.decode(attributes.getValue("title")));
                f.setDownloadPref(Integer.parseInt(attributes.getValue("downloadPref")));
                f.setDisplayPref(Integer.parseInt(attributes.getValue("displayPref")));
                f.setWebScale(Float.parseFloat(attributes.getValue("webScale")));
                f.setFeedScale(Float.parseFloat(attributes.getValue("feedScale")));
                f.setJavaScriptEnabled(Boolean.parseBoolean(attributes.getValue("javaScriptEnabled")));
                try {
                    f.setFitToWidthEnabled(Boolean.parseBoolean(attributes.getValue("fitToWidthEnabled")));
                } catch (RuntimeException rte) {
                // skip as it may be missing. Default is true then.
                }
                f.setNotificationEnabled(Boolean.parseBoolean(attributes.getValue("notificationEnabled")));
                f.setAlternateUrl(attributes.getValue("altUrl"));
                em.insert(f);
            }

            @Override
            public void receivedString(String localTagName, String fullyQualifiedLocalName, String value) {
            }
        };
        parser.parse(new File(fileName), handler);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Log.i("Feed", "Restored feeds. Now " + em.getFeedCount() + " feeds in database.");
    return true;
}
Also used : SimpleStringExtractorHandler(com.newsrob.util.SimpleStringExtractorHandler) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) SdCardStorageAdapter(com.newsrob.storage.SdCardStorageAdapter) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 2 with SdCardStorageAdapter

use of com.newsrob.storage.SdCardStorageAdapter in project newsrob by marianokamp.

the class EntryManager method switchStorageProvider.

private void switchStorageProvider() {
    Log.d(TAG, "Switch Storage Provider");
    if (isModelCurrentlyUpdated())
        return;
    final String newPrefValue = getSharedPreferences().getString(SETTINGS_STORAGE_PROVIDER_KEY, null);
    final String oldStorageProviderClass = fileContextAdapter.getClass().getName();
    final String newStorageProviderClass = STORAGE_PROVIDER_SD_CARD.equals(newPrefValue) ? SdCardStorageAdapter.class.getName() : PhoneMemoryStorageAdapter.class.getName();
    if (!oldStorageProviderClass.equals(newStorageProviderClass)) {
        runningThread = new Thread(new Runnable() {

            public void run() {
                final PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
                final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
                Log.i(TAG, "Wake lock acquired at " + new Date().toString() + ".");
                wl.acquire();
                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                final Timing t = new Timing("Storage Provider Switch", ctx);
                ModelUpdateResult result = null;
                if (isModelCurrentlyUpdated())
                    return;
                try {
                    lockModel("EM.switchStorageProvider.run");
                } catch (final IllegalStateException ise) {
                    return;
                }
                try {
                    Log.i(TAG, "Switching storage providers started at " + new Date().toString() + ".");
                    fireModelUpdateStarted("Switching storage providers", false, true);
                    Log.d(TAG, "Change of storage provider detected.");
                    final List<Job> jobList = new LinkedList<Job>();
                    final Job clearOldStorageProvider = new Job("Clearing Old Storage Provider", EntryManager.this) {

                        @Override
                        public void run() {
                            Log.d(TAG, "Clearing the old storage provider.");
                            doClearCache();
                            if (fileContextAdapter.canWrite())
                                WebPageDownloadDirector.removeAllAssets(fileContextAdapter, ctx);
                        }
                    };
                    jobList.add(clearOldStorageProvider);
                    final Job switchStorageProviders = new Job("Switching Storage Providers", EntryManager.this) {

                        @Override
                        public void run() throws Exception {
                            Log.d(TAG, "Establishing new storage provider: " + newStorageProviderClass);
                            fileContextAdapter = newStorageProviderClass.equals(SdCardStorageAdapter.class.getName()) ? new SdCardStorageAdapter(ctx) : new PhoneMemoryStorageAdapter(ctx);
                            Log.d(TAG, "New storage provider established.");
                        }
                    };
                    jobList.add(switchStorageProviders);
                    final Job clearNewStorageProvider = new Job("Clearing New Storage Provider", EntryManager.this) {

                        @Override
                        public void run() {
                            Log.d(TAG, "Clearing the new storage provider.");
                            doClearCache();
                            if (fileContextAdapter.canWrite())
                                WebPageDownloadDirector.removeAllAssets(fileContextAdapter, ctx);
                        }
                    };
                    jobList.add(clearNewStorageProvider);
                    runJobs(jobList);
                    result = new SwitchStorageProviderResult();
                } catch (final Throwable throwable) {
                    result = new SwitchStorageProviderFailed(throwable);
                    Log.d(TAG, "Problem during switching storage providers.", throwable);
                    t.stop();
                } finally {
                    unlockModel("EM.switchStorageProvider.run");
                    clearCancelState();
                    fireModelUpdateFinished(result);
                    fireStatusUpdated();
                    Log.i(TAG, "Switching storage providers finished at " + new Date().toString() + ".");
                    wl.release();
                    t.stop();
                }
            }
        }, "Storage Provider Switch Worker");
        runningThread.start();
    }
}
Also used : ModelUpdateResult(com.newsrob.jobs.ModelUpdateResult) Date(java.util.Date) LinkedList(java.util.LinkedList) SdCardStorageAdapter(com.newsrob.storage.SdCardStorageAdapter) SwitchStorageProviderResult(com.newsrob.jobs.SwitchStorageProviderResult) PowerManager(android.os.PowerManager) SwitchStorageProviderFailed(com.newsrob.jobs.SwitchStorageProviderFailed) Timing(com.newsrob.util.Timing) Job(com.newsrob.jobs.Job) PhoneMemoryStorageAdapter(com.newsrob.storage.PhoneMemoryStorageAdapter)

Example 3 with SdCardStorageAdapter

use of com.newsrob.storage.SdCardStorageAdapter in project newsrob by marianokamp.

the class Feed method saveFeedSettings.

public static final void saveFeedSettings(Context context) {
    SdCardStorageAdapter storageAdapter = new SdCardStorageAdapter(context.getApplicationContext(), false);
    if (!storageAdapter.canWrite())
        return;
    List<Feed> feeds = EntryManager.getInstance(context).findAllFeeds();
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(storageAdapter.openFileOutput(FEED_SETTINGS_FILE_NAME), 8192))));
        pw.println("<feeds version='1'>");
        for (Feed feed : feeds) {
            StringBuilder sb = new StringBuilder("  <feed ");
            addAttribute(sb, "atomId", URLEncoder.encode(feed.getAtomId()));
            addAttribute(sb, "title", URLEncoder.encode(feed.getTitle()));
            addAttribute(sb, "downloadPref", String.valueOf(feed.getDownloadPref()));
            addAttribute(sb, "displayPref", String.valueOf(feed.getDisplayPref()));
            addAttribute(sb, "webScale", String.valueOf(feed.getWebScale()));
            addAttribute(sb, "feedScale", String.valueOf(feed.getFeedScale()));
            addAttribute(sb, "fitToWidthEnabled", String.valueOf(feed.isFitToWidthEnabled()));
            addAttribute(sb, "javaScriptEnabled", String.valueOf(feed.isJavaScriptEnabled()));
            addAttribute(sb, "notificationEnabled", String.valueOf(feed.isNotificationEnabled()));
            addAttribute(sb, "altUrl", String.valueOf(feed.getAlternateUrl()));
            sb.append("/>");
            pw.println(sb.toString());
        }
        pw.println("</feeds>");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (pw != null)
            pw.close();
    }
    Log.d("Feed", "Saved feeds.");
}
Also used : FileNotFoundException(java.io.FileNotFoundException) OutputStreamWriter(java.io.OutputStreamWriter) BufferedOutputStream(java.io.BufferedOutputStream) SdCardStorageAdapter(com.newsrob.storage.SdCardStorageAdapter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

SdCardStorageAdapter (com.newsrob.storage.SdCardStorageAdapter)3 PowerManager (android.os.PowerManager)1 Job (com.newsrob.jobs.Job)1 ModelUpdateResult (com.newsrob.jobs.ModelUpdateResult)1 SwitchStorageProviderFailed (com.newsrob.jobs.SwitchStorageProviderFailed)1 SwitchStorageProviderResult (com.newsrob.jobs.SwitchStorageProviderResult)1 PhoneMemoryStorageAdapter (com.newsrob.storage.PhoneMemoryStorageAdapter)1 SimpleStringExtractorHandler (com.newsrob.util.SimpleStringExtractorHandler)1 Timing (com.newsrob.util.Timing)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1