2010-04-14 7 views
42

Je regarde le code de la caméra Android, et quand j'essaye d'importer android.os.SystemProperties, il ne peut pas être trouvé.Où est android.os.SystemProperties?

Voici le fichier que je suis à la recherche:
https://android.googlesource.com/platform/packages/apps/Camera/+/eclair-release/src/com/android/camera/VideoCamera.java

J'ai créé un nouveau projet 2.1 et a essayé d'importer à nouveau cet espace de noms, mais il ne peut toujours pas être trouvé. J'ai vérifié https://developer.android.com et SystemProperties n'était pas répertorié.

Ai-je raté quelque chose?

Répondre

29

Ceci est la classe dans le code source Android:

https://android.googlesource.com/platform/frameworks/base/+/eclair-release/core/java/android/os/SystemProperties.java

Voir {@hide} dans la JavaDoc de classe? Cela signifie que cette classe ne sera pas exportée dans le cadre du SDK public.

L'application appareil photo l'utilise de manière interne et n'utilisera pas le SDK public pour le créer.

Vous pouvez toujours être en mesure d'obtenir à cette classe

  1. par réflexion ou

  2. en obtenant la source, la suppression @hide et de faire votre propre SDK personnalisé.

Cependant à peu près, par définition, vous allez maintenant « off SDK » et donc votre application peut ainsi être cassé ou obtenir un comportement différent sur les versions OS que les gens Android feront peu d'effort de ne pas changer cette classe entre les versions.

+3

Si le lien de code source ci-dessus ne fonctionne pas, vous pouvez essayer un http://androidxref.com/4.3_r2.1/xref/frameworks/base/core/java/android/os/SystemProperties.java – Vins

+0

@ Jim Blackler: pourriez-vous s'il vous plaît expliquer plus au sujet de la 2ème solution. comment je peux faire mon propre SDK? –

12

Vous pouvez exec sur la commande getprop:

String line = ""; 
try { 
Process ifc = Runtime.getRuntime().exec("getprop ro.hardware"); 
BufferedReader bis = new BufferedReader(new InputStreamReader(ifc.getInputStream())); 
line = bis.readLine(); 
} catch (java.io.IOException e) { 
} 
ifc.destroy(); 
+0

Il n'y a pas d'IMEI/IMSI dans getprop: https://guardianproject.info/wiki/Google_Nexus_S_I9023_running_stock_4.0.4 –

+0

Voici un exemple par accuya: http://stackoverflow.com/a/11623309/4515489 – jk7

55

Si vous utilisez l'option « réflexion », vous pouvez utiliser la classe ci-dessous

package com.etc.etc; 

import java.io.File; 
import java.lang.reflect.Method; 
import android.content.Context; 
import dalvik.system.DexFile; 


public class SystemPropertiesProxy 
{ 

/** 
* This class cannot be instantiated 
*/ 
private SystemPropertiesProxy(){ 

} 

    /** 
    * Get the value for the given key. 
    * @return an empty string if the key isn't found 
    * @throws IllegalArgumentException if the key exceeds 32 characters 
    */ 
    public static String get(Context context, String key) throws IllegalArgumentException { 

     String ret= ""; 

     try{ 

      ClassLoader cl = context.getClassLoader(); 
      @SuppressWarnings("rawtypes") 
      Class SystemProperties = cl.loadClass("android.os.SystemProperties"); 

      //Parameters Types 
      @SuppressWarnings("rawtypes") 
       Class[] paramTypes= new Class[1]; 
      paramTypes[0]= String.class; 

      Method get = SystemProperties.getMethod("get", paramTypes); 

      //Parameters 
      Object[] params= new Object[1]; 
      params[0]= new String(key); 

      ret= (String) get.invoke(SystemProperties, params); 

     }catch(IllegalArgumentException iAE){ 
      throw iAE; 
     }catch(Exception e){ 
      ret= ""; 
      //TODO 
     } 

     return ret; 

    } 

    /** 
    * Get the value for the given key. 
    * @return if the key isn't found, return def if it isn't null, or an empty string otherwise 
    * @throws IllegalArgumentException if the key exceeds 32 characters 
    */ 
    public static String get(Context context, String key, String def) throws IllegalArgumentException { 

     String ret= def; 

     try{ 

      ClassLoader cl = context.getClassLoader(); 
      @SuppressWarnings("rawtypes") 
      Class SystemProperties = cl.loadClass("android.os.SystemProperties"); 

      //Parameters Types 
      @SuppressWarnings("rawtypes") 
       Class[] paramTypes= new Class[2]; 
      paramTypes[0]= String.class; 
      paramTypes[1]= String.class;   

      Method get = SystemProperties.getMethod("get", paramTypes); 

      //Parameters 
      Object[] params= new Object[2]; 
      params[0]= new String(key); 
      params[1]= new String(def); 

      ret= (String) get.invoke(SystemProperties, params); 

     }catch(IllegalArgumentException iAE){ 
      throw iAE; 
     }catch(Exception e){ 
      ret= def; 
      //TODO 
     } 

     return ret; 

    } 

    /** 
    * Get the value for the given key, and return as an integer. 
    * @param key the key to lookup 
    * @param def a default value to return 
    * @return the key parsed as an integer, or def if the key isn't found or 
    *   cannot be parsed 
    * @throws IllegalArgumentException if the key exceeds 32 characters 
    */ 
    public static Integer getInt(Context context, String key, int def) throws IllegalArgumentException { 

     Integer ret= def; 

     try{ 

      ClassLoader cl = context.getClassLoader(); 
      @SuppressWarnings("rawtypes") 
      Class SystemProperties = cl.loadClass("android.os.SystemProperties"); 

      //Parameters Types 
      @SuppressWarnings("rawtypes") 
       Class[] paramTypes= new Class[2]; 
      paramTypes[0]= String.class; 
      paramTypes[1]= int.class; 

      Method getInt = SystemProperties.getMethod("getInt", paramTypes); 

      //Parameters 
      Object[] params= new Object[2]; 
      params[0]= new String(key); 
      params[1]= new Integer(def); 

      ret= (Integer) getInt.invoke(SystemProperties, params); 

     }catch(IllegalArgumentException iAE){ 
      throw iAE; 
     }catch(Exception e){ 
      ret= def; 
      //TODO 
     } 

     return ret; 

    } 

    /** 
    * Get the value for the given key, and return as a long. 
    * @param key the key to lookup 
    * @param def a default value to return 
    * @return the key parsed as a long, or def if the key isn't found or 
    *   cannot be parsed 
    * @throws IllegalArgumentException if the key exceeds 32 characters 
    */ 
    public static Long getLong(Context context, String key, long def) throws IllegalArgumentException { 

     Long ret= def; 

     try{ 

      ClassLoader cl = context.getClassLoader(); 
      @SuppressWarnings("rawtypes") 
       Class SystemProperties= cl.loadClass("android.os.SystemProperties"); 

      //Parameters Types 
      @SuppressWarnings("rawtypes") 
       Class[] paramTypes= new Class[2]; 
      paramTypes[0]= String.class; 
      paramTypes[1]= long.class; 

      Method getLong = SystemProperties.getMethod("getLong", paramTypes); 

      //Parameters 
      Object[] params= new Object[2]; 
      params[0]= new String(key); 
      params[1]= new Long(def); 

      ret= (Long) getLong.invoke(SystemProperties, params); 

     }catch(IllegalArgumentException iAE){ 
      throw iAE; 
     }catch(Exception e){ 
      ret= def; 
      //TODO 
     } 

     return ret; 

    } 

    /** 
    * Get the value for the given key, returned as a boolean. 
    * Values 'n', 'no', '0', 'false' or 'off' are considered false. 
    * Values 'y', 'yes', '1', 'true' or 'on' are considered true. 
    * (case insensitive). 
    * If the key does not exist, or has any other value, then the default 
    * result is returned. 
    * @param key the key to lookup 
    * @param def a default value to return 
    * @return the key parsed as a boolean, or def if the key isn't found or is 
    *   not able to be parsed as a boolean. 
    * @throws IllegalArgumentException if the key exceeds 32 characters 
    */ 
    public static Boolean getBoolean(Context context, String key, boolean def) throws IllegalArgumentException { 

     Boolean ret= def; 

     try{ 

      ClassLoader cl = context.getClassLoader(); 
      @SuppressWarnings("rawtypes") 
      Class SystemProperties = cl.loadClass("android.os.SystemProperties"); 

      //Parameters Types 
      @SuppressWarnings("rawtypes") 
       Class[] paramTypes= new Class[2]; 
      paramTypes[0]= String.class; 
      paramTypes[1]= boolean.class; 

      Method getBoolean = SystemProperties.getMethod("getBoolean", paramTypes); 

      //Parameters   
      Object[] params= new Object[2]; 
      params[0]= new String(key); 
      params[1]= new Boolean(def); 

      ret= (Boolean) getBoolean.invoke(SystemProperties, params); 

     }catch(IllegalArgumentException iAE){ 
      throw iAE; 
     }catch(Exception e){ 
      ret= def; 
      //TODO 
     } 

     return ret; 

    } 

    /** 
    * Set the value for the given key. 
    * @throws IllegalArgumentException if the key exceeds 32 characters 
    * @throws IllegalArgumentException if the value exceeds 92 characters 
    */ 
    public static void set(Context context, String key, String val) throws IllegalArgumentException { 

     try{ 

      @SuppressWarnings("unused") 
      DexFile df = new DexFile(new File("/system/app/Settings.apk")); 
      @SuppressWarnings("unused") 
      ClassLoader cl = context.getClassLoader(); 
      @SuppressWarnings("rawtypes") 
      Class SystemProperties = Class.forName("android.os.SystemProperties"); 

      //Parameters Types 
      @SuppressWarnings("rawtypes") 
       Class[] paramTypes= new Class[2]; 
      paramTypes[0]= String.class; 
      paramTypes[1]= String.class; 

      Method set = SystemProperties.getMethod("set", paramTypes); 

      //Parameters   
      Object[] params= new Object[2]; 
      params[0]= new String(key); 
      params[1]= new String(val); 

      set.invoke(SystemProperties, params); 

     }catch(IllegalArgumentException iAE){ 
      throw iAE; 
     }catch(Exception e){ 
      //TODO 
     } 

    } 
} 
+0

Est-ce que cela fonctionne? J'ai essayé SystemPropertiesProxy.get (this, "android.telephony.TelephonyProperties.PROPERTY_IMSI"); mais vient vide. –

+0

@ bruno.braga, ce n'est pas ce genre de propriété qui est obtenue à partir de SystemProperties. Pour voir les propriétés que vous pouvez obtenir de votre appareil de cette manière, lancez 'adb shell getprop'. – lapis

+0

@Psycho, ouais je sais ... c'est le point ... pour aucun des appareils que j'ai testés, c'est disponible dans getprop. C'est pourquoi je pense que cela ne marche tout simplement pas. –

14

Après beaucoup de déconner je J'ai enfin obtenu le code de réflexion ci-dessus qui fonctionne à la fois pour définir et créer de nouvelles propriétés système natives, il y a quelques mises en garde:

  1. Vous devez être l'utilisateur du système, ajoutez: android: sharedUserId = "android.uid.system" au manifeste.

  2. Vous devez signer votre APK avec la clé de la plate-forme, je triché et juste l'emportait sur la clé de signature de débogage par défaut dans Eclipse comme le montrent ici: http://stoned-android.blogspot.co.uk/2012_01_01_archive.html

  3. Les propriétés du système natif service a une ACL qui contrôle tous les écrire l'accès aux propriétés que vous pouvez subvertir un espace clé (tel que sys ou déboguer.). Voir /system/core/init/property_service.c:

    { "net", AID_SYSTEM, 0}, { "dev.", AID_SYSTEM, 0}, { "exécution.", AID_SYSTEM, 0 }, {"hw.», AID_SYSTEM, 0}, { "sys.", AID_SYSTEM, 0}, { "service.", AID_SYSTEM, 0}, { "wlan.", AID_SYSTEM, 0}, { "dhcp.", AID_SYSTEM, 0},

Ou si vous roulez votre propre construction, vous pouvez ajouter votre propre clé si vous vouliez vraiment, mais il semble plus facile de réutiliser l'un des ci-dessus.

21

la classe affichée en tant réponse de Void utilisateur a un tas de choses inutiles Voici ma classe qui utilise la réflexion sur android.os.SystemProperties.

/* 
* Copyright (C) 2015 Jared Rummler 
* 
* 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. 
*/ 

/** 
* Gives access to the system properties store. The system properties store contains a list of 
* string key-value pairs. 
*/ 
public class SystemProperties { 

    private static final Class<?> SP = getSystemPropertiesClass(); 

    /** 
    * Get the value for the given key. 
    */ 
    public static String get(String key) { 
    try { 
     return (String) SP.getMethod("get", String.class).invoke(null, key); 
    } catch (Exception e) { 
     return null; 
    } 
    } 

    /** 
    * Get the value for the given key. 
    * 
    * @return if the key isn't found, return def if it isn't null, or an empty string otherwise 
    */ 
    public static String get(String key, String def) { 
    try { 
     return (String) SP.getMethod("get", String.class, String.class).invoke(null, key, def); 
    } catch (Exception e) { 
     return def; 
    } 
    } 

    /** 
    * Get the value for the given key, returned as a boolean. Values 'n', 'no', '0', 'false' or 
    * 'off' are considered false. Values 'y', 'yes', '1', 'true' or 'on' are considered true. (case 
    * sensitive). If the key does not exist, or has any other value, then the default result is 
    * returned. 
    * 
    * @param key 
    *  the key to lookup 
    * @param def 
    *  a default value to return 
    * @return the key parsed as a boolean, or def if the key isn't found or is not able to be 
    * parsed as a boolean. 
    */ 
    public static boolean getBoolean(String key, boolean def) { 
    try { 
     return (Boolean) SP.getMethod("getBoolean", String.class, boolean.class) 
      .invoke(null, key, def); 
    } catch (Exception e) { 
     return def; 
    } 
    } 

    /** 
    * Get the value for the given key, and return as an integer. 
    * 
    * @param key 
    *  the key to lookup 
    * @param def 
    *  a default value to return 
    * @return the key parsed as an integer, or def if the key isn't found or cannot be parsed 
    */ 
    public static int getInt(String key, int def) { 
    try { 
     return (Integer) SP.getMethod("getInt", String.class, int.class).invoke(null, key, def); 
    } catch (Exception e) { 
     return def; 
    } 
    } 

    /** 
    * Get the value for the given key, and return as a long. 
    * 
    * @param key 
    *  the key to lookup 
    * @param def 
    *  a default value to return 
    * @return the key parsed as a long, or def if the key isn't found or cannot be parsed 
    */ 
    public static long getLong(String key, long def) { 
    try { 
     return (Long) SP.getMethod("getLong", String.class, long.class).invoke(null, key, def); 
    } catch (Exception e) { 
     return def; 
    } 
    } 

    private static Class<?> getSystemPropertiesClass() { 
    try { 
     return Class.forName("android.os.SystemProperties"); 
    } catch (ClassNotFoundException shouldNotHappen) { 
     return null; 
    } 
    } 

    private SystemProperties() { 
    throw new AssertionError("no instances"); 
    } 

} 
+0

Je vois que la méthode set ne fonctionne pas. Mon appareil a été enraciné. Lorsque je lance votre code, l'application SuperSU n'apparaît pas dans la boîte de dialogue pour obtenir l'autorisation. Donc je pense que votre code ne déclenche pas réellement l'autorisation su. – hqt

+0

@hqt vous devrez exécuter setprop dans un shell. Je devrais supprimer la méthode setprop. –

1

Après beaucoup de recherches, j'ai trouvé un moyen de définir la propriété système pour Android. Je n'ai pas trouvé de solution pour la version Android Lollipop. Mais j'ai réussi à le faire. Pour définir la propriété du système, nous devons utiliser:

import android.os.SystemProperties 
SystemProperties.set(key, value). 

par exemple. SystemProperties.set("sys.android", 5.0)

Maintenant, vous devez donner des autorisations à la nouvelle propriété système Aller à /home/inkkashy04/Android_Lollypop/external/sepolicy/property_contexts et donner l'autorisation appropriée à votre propriété

sys.android u: object_r: system_prop: s0

maintenant, après avoir flashé votre image, vous pouvez voir vos propriétés répertoriées par commande:

adb shell getprop