com.handlerexploit.news.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.handlerexploit.news.MainActivity.java

Source

/*
 *  Copyright (c) 2011 Daniel Huckaby
 *
 *  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.
 */
package com.handlerexploit.news;

import java.io.File;
import java.util.Date;

import com.handlerexploit.news.adapters.PagerAdapter;
import com.handlerexploit.news.data.ArticlesProvider;
import com.handlerexploit.news.data.WeatherProvider;
import com.handlerexploit.news.data.ArticlesProvider.Section;
import com.handlerexploit.news.fragments.ArticlesFragment;
import com.handlerexploit.news.fragments.WeatherFragment;
import com.handlerexploit.news.widgets.PagerHeader;

import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.format.DateUtils;

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final String defaultInternalCacheDirectory = Environment.getDataDirectory().getPath() + "/data/"
                + getPackageName() + "/databases/";

        ArticlesProvider.init(defaultInternalCacheDirectory);
        WeatherProvider.init(defaultInternalCacheDirectory);

        // TODO
        new Thread(new Runnable() {
            public void run() {
                clearFolder(new File(defaultInternalCacheDirectory), 1);
                runOnUiThread(new Runnable() {
                    public void run() {
                        setupViews();
                    }
                });
            }
        }).start();
    }

    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Make this better
    menu.add("Test").setIcon(android.R.drawable.ic_menu_crop);
    return super.onCreateOptionsMenu(menu);
    }*/

    private void setupViews() {
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);

        PagerAdapter adapter = new PagerAdapter(this, viewPager, (PagerHeader) findViewById(R.id.pagerHeader));

        adapter.addPage(WeatherFragment.class, null, "Weather");

        for (Section section : Section.values()) {
            Bundle params = new Bundle();
            params.putString("section", section.name());
            adapter.addPage(ArticlesFragment.class, params, section.label);
        }

        if (adapter.getCount() > 1) {
            viewPager.setCurrentItem(1, false);
        }
    }

    public void clearFolder(final File directory, int numDays) {
        if (directory != null && directory.isDirectory()) {
            for (File child : directory.listFiles()) {
                try {
                    if (child.isDirectory()) {
                        clearFolder(child, numDays);
                    }

                    if (child.lastModified() < new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
                        child.delete();
                    }
                } catch (Throwable ignored) {
                }
            }
        }
    }
}