2013年9月12日 星期四

get/set properties in Android

I tried to use setprop/getprop shell commands to test if I could set/get a defined key to a system's permanent storage, but it didn't do that instead the key is gone after reboot. I then turned to another solution after researching the internet. I create a properties file in the data folder and read it into a Properties object. I can operate on  the object and save back the properties after finish. The following is the snippet I wrote after referring to the red link in the reference links below:

public class HotkeyListAdapter extends ArrayAdapter<Object> {
    int total_hotkey_count = 8;
    Context context;
    static String filePath = "/data/hotkey.properties";
    String hotkey_map[] = {
        "A", "B", "C", "D", "E", "F", "G", "H"
    };
    static String targetApp = "com.android.settings";   
    static String TAG = "HotkeyListAdapter";

    public HotkeyListAdapter(Context context, int resourceId) {
        super(context, resourceId);
        this.context = context;
    }

    @Override
    public int getCount() {
        return total_hotkey_count;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static Properties loadPropties() throws IOException {
        Properties prop = new Properties();
        try {
            InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
            prop.load(fileStream);
            fileStream.close();
        } 
     catch (FileNotFoundException e) {
            Log.e(TAG , "Got exception " + e);
        }
        return prop;
    }


    static void storePropties(Properties prop) throws IOException {
        try {
            OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(filePath));
            prop.store(fileStream, "Hotkey mappings");
            fileStream.close();
        } 
     catch (FileNotFoundException e) {
            Log.e(TAG , "Got exception " + e);
        }
    }


    static String getProperties(String key) {
     Properties prop;
        try {
            prop = loadPropties();
         return prop.getProperty(key, "NA");
        }
     catch (IOException e) {
            Log.e(TAG, "Exception", e);
        }
     return "NA";
    }

    static void setProperties(String key, String value) {
     Properties prop;   
        try {
         prop = loadPropties();
         prop.setProperty(key, value);
            storePropties(prop);
        }
     catch (IOException e) {
            Log.e(TAG, "Exception", e);
        }
     return;   
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView appImage;
     TextView keyName, appName;   
     Intent hotkey_intent;
     List<ResolveInfo> resolveinfo_list;
     PackageManager pm = context.getPackageManager();
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.hotkey_list_row, null);    
        }   

        appImage = (ImageView) convertView.findViewById(R.id.hotkey_app_image);
     appName = (TextView) convertView.findViewById(R.id.hotkey_app_name);
     keyName = (TextView) convertView.findViewById(R.id.hotkey_name);

        /* Retrieve APP info */
     targetApp = getProperties("hotkey"+position);   
     Log.d(TAG, targetApp);
   
        hotkey_intent = new Intent("android.intent.action.MAIN");
        hotkey_intent.addCategory("android.intent.category.LAUNCHER");
     resolveinfo_list = context.getPackageManager().queryIntentActivities(hotkey_intent, 0);
        for(ResolveInfo info:resolveinfo_list){
                 if(info.activityInfo.packageName.equalsIgnoreCase(targetApp)){    
                  ApplicationInfo ai;
                     appImage.setImageDrawable(info.activityInfo.loadIcon(pm));
            try {
                         ai = pm.getApplicationInfo(info.activityInfo.packageName, 0);
                     }
            catch (final NameNotFoundException e) {
                         ai = null;
                     }
            appName.setText((ai == null ? "not found" : pm.getApplicationLabel(ai)));
            keyName.setText(hotkey_map[position]);
                     break;
                 }
        }
       
        return convertView;
    }
}


public class HotkeyAppListDialog extends DialogFragment {
    private AlertDialog mDialog;
    private static HotkeyListAdapter hotkey_list_adapter;
    private static int hotkey_num;
   
    public static HotkeyAppListDialog newInstance(int position, HotkeyListAdapter adapter) {
        HotkeyAppListDialog frag = new HotkeyAppListDialog();
     hotkey_list_adapter = adapter;
     hotkey_num = position;
        return frag;
    }
   
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     HotkeyAppListAdapter adapter = new HotkeyAppListAdapter(getActivity(), R.layout.hotkey_app_list_row);
     ListView appList = new ListView(getActivity());   
     appList.setAdapter(adapter);
     appList.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     appList.setOnItemClickListener(new OnItemClickListener() {   
            public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
          HotkeyListAdapter.setProperties("hotkey"+hotkey_num, (String)view.getTag());
          hotkey_list_adapter.notifyDataSetChanged();
                mDialog.cancel();
            }
        });
        builder.setView(appList);
        builder.setTitle(R.string.hotkey_app_list_title);  
     mDialog = builder.create();
        return mDialog;
    }
}


Reference links
http://stackoverflow.com/questions/14450714/how-do-i-read-a-property-from-project-properties-or-local-properties
Architecture
http://rxwen.blogspot.tw/2010/01/android-property-system.html
worth reading
http://hi.baidu.com/seucrcr/item/2b988c570cb63c9208be1778
http://bininda.com/blog/tag/android/


沒有留言:

張貼留言