jeudi 8 janvier 2015

Android su commands not working when called for modifying /system in android

What i basically have is a simple android navigation drawer with some fragments , i have a checkbox called from a Fragment placed in the main activity which then when clicked executes a root command that should modify the build.prop file in /system but unfortunately it doesn't seem to work and i get this in the logs -->



D/su (17546): su invoked.
D/su (17546): starting daemon client 10129 10129
D/su (17548): remote pid: 17546
D/su (17548): remote pts_slave:
D/su (17548): waiting for child exit
D/su (17550): su invoked.
D/su (17550): db allowed
D/su (17550): 10129 /system/xbin/su executing 0 /system/bin/sh using binary
/system/bin/sh : sh
D/su (17548): sending code
D/su (17548): child exited
D/su (17546): client exited 0
D/AndroidRuntime(17551):
D/AndroidRuntime(17551): >>>>>> AndroidRuntime START com.android.internal.os.Run
timeInit <<<<<<
D/AndroidRuntime(17551): CheckJNI is OFF
D/AndroidRuntime(17551): Calling main entry com.android.commands.am.Am


here is the code of my Activity (AboutActivity)



package com.adromo.tweaker.activities;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.GravityCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

import com.adromo.tweaker.R;
import com.adromo.tweaker.fragments.InitD;
import com.adromo.tweaker.fragments.StartFragment;
import com.adromo.tweaker.widget.CustomDrawerLayout;
import com.ikimuhendis.ldrawer.ActionBarDrawerToggle;
import com.ikimuhendis.ldrawer.DrawerArrowDrawable;

import org.sufficientlysecure.rootcommands.Shell;
import org.sufficientlysecure.rootcommands.command.SimpleCommand;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeoutException;

public class AboutActivity extends FragmentActivity {

public static Context appContext;

//==================================
// Drawer
//==================================
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";

private ActionBarDrawerToggle mDrawerToggle;
private CustomDrawerLayout mDrawerLayout;
private ListView mDrawerListView;
private View mFragmentContainerView;
private int mCurrentSelectedPosition = 0;
private boolean mFromSavedInstanceState;
private ListView mDrawerList;
private DrawerArrowDrawable drawerArrow;
private boolean drawerArrowColor;

private static int DRAWER_MODE = 0;
private SharedPreferences mPreferences;

private static final int MENU_BACK = Menu.FIRST;

String titleString[];

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

appContext = getApplicationContext();

setContentView(R.layout.drawer_main);

mDrawerListView = (ListView) findViewById(R.id.dw_navigation_drawer);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});

mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
R.layout.drawer_list,
android.R.id.text1,
getTitles()));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);

setUpNavigationDrawer(
findViewById(R.id.dw_navigation_drawer),
(CustomDrawerLayout) findViewById(R.id.dw_drawer_layout));

ActionBar ab = getActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);

if (savedInstanceState != null) {
mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
mFromSavedInstanceState = true;
}

File folder = new File(Environment.getExternalStorageDirectory() + "/Adromo");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}


CopyAssets();
}



private void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("Files");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}

for(String filename : files) {
System.out.println("File name => "+filename);
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("Files/"+filename); // if files resides inside the "Files" directory itself
out = new FileOutputStream("/sdcard/Adromo/" + filename );
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}

@Override
public void onResume() {
super.onResume();
}

@Override
public void onPause() {
super.onPause();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_BACK, 0, R.string.toggle_back_cfibers)
.setIcon(R.drawable.ic_back)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
restoreActionBar();
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}

switch (item.getItemId()) {
case MENU_BACK:
onBackPressed();
return true;
default:
return super.onContextItemSelected(item);
}
}

//==================================
// Methods
//==================================

/**
* Users of this fragment must call this method to set up the
* navigation menu_drawer interactions.
*
* @param fragmentContainerView The view of this fragment in its activity's layout.
* @param drawerLayout The DrawerLayout containing this fragment's UI.
*/
public void setUpNavigationDrawer(View fragmentContainerView, CustomDrawerLayout drawerLayout) {
mFragmentContainerView = fragmentContainerView;
mDrawerLayout = drawerLayout;

mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

drawerArrow = new DrawerArrowDrawable(this) {
@Override
public boolean isLayoutRtl() {
return false;
}
};
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
drawerArrow, R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {

public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}

public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};

mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();

mDrawerToggle.setDrawerIndicatorEnabled(true);

if (!mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}

mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});

mDrawerLayout.setDrawerListener(mDrawerToggle);

selectItem(mCurrentSelectedPosition);
}

public boolean isDrawerOpen() {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}

/**
* Restores the action bar after closing the menu_drawer
*/
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setTitle(getTitle());
}

private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.dw_container, PlaceholderFragment.newInstance(getPosition(position)))
.commit();
}

/**
* Depending on if the item is shown or not, it increases
* the position to make the activity load the right fragment.
*
* @param pos The selected position
* @return the modified position
*/
public int getPosition(int pos) {
int position = pos;
switch (DRAWER_MODE) {
default:
case 0:
position = pos;
break;
case 1:
if (pos > 0) position = pos + 1;
break;
case 2:
if (pos > 3) position = pos + 1;
break;
case 3:
if (pos > 0) position = pos + 1;
if (pos > 3) position = pos + 2;
break;
}
return position;
}

/**
* Get a list of titles for the tabstrip to display depending on if the
* voltage control fragment and battery fragment will be displayed. (Depends on the result of
* Helpers.voltageTableExists() & Helpers.showBattery()
*
* @return String[] containing titles
*/
private String[] getTitles() {
String titleString[];
DRAWER_MODE = 0;
titleString = new String[]{
getString(R.string.start_title),
getString(R.string.navdraw_initd),};
return titleString;
}

//==================================
// Internal Classes
//==================================

public static final int FRAGMENT_ID_StartFragment = 0;
public static final int FRAGMENT_ID_InitD = 1;


public static class PlaceholderFragment extends Fragment {

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static Fragment newInstance(int fragmentId) {
Fragment fragment;
switch (fragmentId) {
default:
case FRAGMENT_ID_StartFragment:
fragment = new StartFragment();
break;
case FRAGMENT_ID_InitD:
fragment = new InitD();
break;
}

return fragment;
}

public PlaceholderFragment() {
// intentionally left blank
}
}

// begin initd checkboxes

public void initd1(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();

// Check which checkbox was clicked
switch(view.getId()) {
case R.id.initd1:
if (checked){

try {
Shell shell = Shell.startRootShell();
SimpleCommand command2 = new SimpleCommand("su -c cp /system/build.prop /system/build.prop.bak");
shell.add(command2).waitForFinish();
shell.close();

} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}

} else {

Toast.makeText(getApplicationContext(), "LOL",
Toast.LENGTH_SHORT).show();

}
// Remove the meat
break;
}
}
}


here is the code of my fragment



/*
* Copyright (C) 2013 Carbon Development
*
* 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://ift.tt/jtTJvY
*
* 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.adromo.tweaker.fragments;

import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.adromo.tweaker.R;

public class InitD extends PreferenceFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.initd, container, false);


}

}


the library i am using is called rootcommands (i think this shouldn't matter as every library i try gives me the same exact result) i am running android lolipop 5.0.1 on an SGS4





Aucun commentaire:

Enregistrer un commentaire