com.github.punkboy.ampdroid.ui.AMPDroidBaseActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.github.punkboy.ampdroid.ui.AMPDroidBaseActivity.java

Source

/*
 * Copyright 2013 Shushant.
 *
 * 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.github.punkboy.ampdroid.ui;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.io.FileUtils;

import android.annotation.TargetApi;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;

import com.actionbarsherlock.app.SherlockActivity;
import com.github.punkboy.ampdroid.R;

public abstract class AMPDroidBaseActivity extends SherlockActivity {

    final private static String TAG_BASE = "AMPDroidBaseActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
        StrictModePermitAll();

    }

    /*
     * Internal path to a directory assigned to the package for its persistent
     * data.
     */

    final public String getAppDirectory() {

        return getApplicationInfo().dataDir.toString();

    }

    /*
     * External path to a directory assigned to the <b>htdocs</b>.
     */

    final public String getHttpDirectory() {

        return Environment.getExternalStorageDirectory().getPath() + "/htdocs/";

    }

    /*
     * StrictModePermitAll will grand all permission which is caused by
     * accidentally. Very Useful on Development Mode
     */

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    private final void StrictModePermitAll() {
        StrictMode.setThreadPolicy((new android.os.StrictMode.ThreadPolicy.Builder()).permitAll().build());
        Log.d(TAG_BASE, "StrictMode is Currently set to PermitAll");
    }

    private void copyConfigFromAssets(String fileName) {

        File isConf = new File(getHttpDirectory() + "/conf/" + fileName);

        if (!isConf.exists()) {

            try {

                File mFile = new File(getHttpDirectory() + "/tmp/");

                if (!mFile.exists())
                    mFile.mkdir();

                String mString;

                InputStream mStream = getAssets().open(fileName, AssetManager.ACCESS_BUFFER);

                BufferedWriter outputStream = new BufferedWriter(
                        new FileWriter(getHttpDirectory() + "/tmp/" + fileName));

                int c;
                while ((c = mStream.read()) != -1) {
                    outputStream.write(c);
                }
                outputStream.close();
                mStream.close();

                mString = FileUtils.readFileToString(new File(getHttpDirectory() + "/tmp/" + fileName), "UTF-8");

                mString = mString.replace("%app_dir%", getAppDirectory());
                mString = mString.replace("%http_dir%", getHttpDirectory());
                FileUtils.writeStringToFile(new File(getHttpDirectory() + "/conf/" + fileName), mString, "UTF-8");
            } catch (Exception e) {
                Log.e(TAG_BASE, "Unable to copy " + fileName + " from assets", e);

            }
        } else {
            Log.d(TAG_BASE, "Config file " + fileName + " already exist no need to overwrite");
        }

    }

    protected void updateSettings() {
        if (isExecutableDataExist()) {
            File mFile = new File(getHttpDirectory() + "/conf/");
            if (!mFile.exists())
                mFile.mkdirs();

            mFile = new File(getHttpDirectory() + "/www/");

            if (!mFile.exists())
                mFile.mkdir();
            mFile = new File(getHttpDirectory() + "/logs/");
            if (!mFile.exists())
                mFile.mkdir();
            mFile = new File(getHttpDirectory() + "/tmp/");
            if (!mFile.exists())
                mFile.mkdir();
            mFile = new File(getHttpDirectory() + "/proc/");
            if (!mFile.exists())
                mFile.mkdir();

            copyConfigFromAssets("lighttpd.conf");
            copyConfigFromAssets("php.ini");
            copyConfigFromAssets("mysql.ini");
        } else {
            Log.e(TAG_BASE, "Executable binaries does not exist");

        }

    }

    public boolean isExecutableDataExist() {

        File mPhp = new File(getAppDirectory() + "/php-cgi");
        File mMySql = new File(getAppDirectory() + "/mysqld");
        File mLighttpd = new File(getAppDirectory() + "/lighttpd");
        File mMySqlMon = new File(getAppDirectory() + "/mysql-monitor");
        File mSendMail = new File(getAppDirectory() + "/msmtp/bin/msmtp");

        if (mPhp.exists() && mMySql.exists() && mLighttpd.exists() && mMySqlMon.exists() && mSendMail.exists()) {

            return true;

        }

        return false;

    }

    public void setPermission(/* Runtime mRuntime */) {
        // Runtime mRuntime = Runtime.getRuntime();
        try {

            exec("/system/bin/chmod 777 " + getAppDirectory() + "/lighttpd");
            exec("/system/bin/chmod 777 " + getAppDirectory() + "/php-cgi");
            exec("/system/bin/chmod 777 " + getAppDirectory() + "/mysqld");
            exec("/system/bin/chmod 777 " + getAppDirectory() + "/mysql-monitor");
            exec("/system/bin/chmod 777 " + getAppDirectory() + "/msmtp/bin/msmtp");

        } catch (Exception e) {
            Log.e(TAG_BASE, "setPermission", e);
        }

    }

    final protected String exec(String cmd) {

        try {
            // Executes the command.
            Process process = Runtime.getRuntime().exec(cmd);

            // NOTE: You can write to stdin of the command using
            // process.getOutputStream().
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[4096 * 12];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();

            // Waits for the command to finish.
            process.waitFor();

            return output.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    protected void runServer(ProcessBuilder serverProc, ProcessBuilder mysqlProc) {

        return;
    }
}