/*
 * Copyright (C) 2017 Team Horizon
 *
 * 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.xenonota;

import android.app.Fragment;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.xenonota.R;
import com.xenonota.configs.LinkConfig;
import com.xenonota.dialogs.WaitDialogFragment;
import com.xenonota.fragments.XenonOTAFragment;

import java.io.IOException;

public class MainActivity extends PreferenceActivity implements
        WaitDialogFragment.OTADialogListener, LinkConfig.LinkConfigListener {

    private static final String FRAGMENT_TAG = XenonOTAFragment.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        XenonOTAFragment mFragment = (XenonOTAFragment) getFragmentManager().findFragmentByTag(FRAGMENT_TAG);
        if (mFragment == null) {
            getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, new XenonOTAFragment(), FRAGMENT_TAG)
                    .commit();
        }
        assert getActionBar() != null;
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.reboot_recovery, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                finish();
                return true;
            case R.id.recovery:
                try {
                    Process revovery = Runtime.getRuntime().exec("reboot recovery");
                } catch (IOException e) {
                   e.printStackTrace();
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onProgressCancelled() {
        Fragment fragment = getFragmentManager().findFragmentByTag(FRAGMENT_TAG);
        if (fragment instanceof WaitDialogFragment.OTADialogListener) {
            ((WaitDialogFragment.OTADialogListener) fragment).onProgressCancelled();
        }
    }

    @Override
    public void onConfigChange() {
        Fragment fragment = getFragmentManager().findFragmentByTag(FRAGMENT_TAG);
        if (fragment instanceof LinkConfig.LinkConfigListener) {
            ((LinkConfig.LinkConfigListener) fragment).onConfigChange();
        }
    }
}

@@ -618,16 +618,45 @@
 onota;%0A%0A
+import android.app.Activity;%0A
 import a
@@ -702,16 +702,48 @@
 Bundle;%0A
+import android.os.PowerManager;%0A
 import a
@@ -2289,95 +2289,110 @@
     
-try %7B%0A                    Process revovery = Runtime.getRuntime().exec(%22
+    ((PowerManager) getApplicationContext().getSystemService(Activity.POWER_SERVICE)).
 reboot
- 
+(%22
 recovery
 %22);%0A
@@ -2391,111 +2391,18 @@
 very
-%22);%0A                %7D catch (IOException e) %7B%0A                   e.printStackTrace();%0A                %7D
+-update%22);
 %0A   

Use PowerManager to reboot