首页 > 代码库 > qsettings

qsettings

f:\linux\android\leon\workspace\qsettings\res\xml\settings_headers.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <Preference
        android:key="profiles_settings"
        android:title="@string/profiles_settings_title" 
        android:persistent="false" />
    <Preference
        android:key="tether_settings"
        android:title="@string/tether_settings_title_all" 
        android:persistent="false" />
    <Preference
        android:key="development_settings"
        android:title="@string/development_settings_title" 
        android:persistent="false" />
    <Preference
        android:key="all_settings"
        android:title="@string/all_settings_title" 
        android:persistent="false" />
    
</PreferenceScreen>

f:\linux\android\leon\workspace\qsettings\src\com\ango\qsettings\QSettings.java

package com.ango.qsettings;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;


public class QSettings extends PreferenceActivity {

    private static final String LOG_TAG = "QSettings";
    private static final String USB_TETHER_SETTINGS = "usb_tether_settings";


    private static final int INVALID             = -1;
    private static final int WIFI_TETHERING      = 0;
    private static final int USB_TETHERING       = 1;
    private static final int BLUETOOTH_TETHERING = 2;
    public static final int TETHER_ERROR_NO_ERROR           = 0;

    /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
    private int mTetherChoice = INVALID;

    private Preference mProfiles;
    private Preference mUsbTether;
    private Preference mEnableAdb;
    private Preference mAllSettings;

    private BroadcastReceiver mTetherChangeReceiver;


//    private String[] mUsbRegexs;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings_headers);

        mProfiles =  findPreference("profiles_settings");
        mUsbTether =  findPreference("tether_settings");
        mEnableAdb =  findPreference("development_settings");
        mAllSettings =  findPreference("all_settings");

//        ConnectivityManager cm =
//                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);


//        mUsbRegexs = cm.getTetherableUsbRegexs();
//		setContentView(R.layout.activity_qsettings);
//
//		if (savedInstanceState == null) {
//			getSupportFragmentManager().beginTransaction()
//					.add(R.id.container, new PlaceholderFragment()).commit();
//		}
	}

    /**
     * Populate the activity with the top-level headers.
     */
    @Override
    public void onBuildHeaders(List<Header> headers) {
//        loadHeadersFromResource(R.xml.settings_headers, headers);

//        updateHeaderList(headers);
    }


    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
        ConnectivityManager cm =
                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

//        if (preference == mUsbTether) {
//            boolean newState = mUsbTether.isChecked();
//
//            if (newState) {
//            	mTetherChoice = USB_TETHERING;
//            	try {
//					startTethering();
//				} catch (IllegalArgumentException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (NoSuchMethodException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (IllegalAccessException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (InvocationTargetException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//            } else {
//                try {
//					setUsbTethering(newState);
//				} catch (IllegalArgumentException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (NoSuchMethodException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (IllegalAccessException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (InvocationTargetException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//            }
//        }     
        if (preference == mUsbTether) {
//            Intent intent = Intent.makeRestartActivityTask(
//                    new ComponentName("com.android.settings",
//                            "com.android.settings.SubSettings"));
//            intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, "com.android.settings.deviceinfo.Memory");
//            intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_TITLE, "tether_settings");
//            intent.putExtra(PreferenceActivity.EXTRA_NO_HEADERS, true);
//            //startActivity(intent);
//            startActivityForResult( intent , 0); 
			Intent intent=new Intent("/");
			ComponentName cn=new ComponentName("com.android.settings", "com.android.settings.TetherSettings");
			intent.setComponent(cn);
			startActivityForResult(intent, 1);
        }  else if (preference == mEnableAdb) {
			Intent intent=new Intent("/");
			ComponentName cn=new ComponentName("com.android.settings", "com.android.settings.DevelopmentSettings");
			intent.setComponent(cn);
			startActivityForResult(intent, 1);
        	
        }  else if (preference == mAllSettings) {
			Intent intent=new Intent("/");
			ComponentName cn=new ComponentName("com.android.settings", "com.android.settings.Settings");
			intent.setComponent(cn);
			startActivityForResult(intent, 1);
        	
        } else if (preference == mProfiles) {
			Intent intent=new Intent("/");
			ComponentName cn=new ComponentName("com.android.settings", "com.android.settings.profile.ProfileSettings");
//			ComponentName cn=new ComponentName("com.android.settings", ".profile.ProfileSettings");
			intent.setComponent(cn);
			startActivityForResult(intent, 1);
			//intent.setClassName("com.android.settings", "com.android.settings.profile.ProfileSettings");
			//mContext.startActivity(intent);
        }

        return super.onPreferenceTreeClick(screen, preference);
    }    

    private void startTethering() throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        switch (mTetherChoice) {
            case USB_TETHERING:
                setUsbTethering(true);
                break;
            default:
                //should not happen
                break;
        }
    }


    private void setUsbTethering(boolean enabled) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        ConnectivityManager cm =
            (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        Class<?> cmClass = cm.getClass();
        Method method = cmClass.getMethod("setUsbTethering", boolean.class);
        method.invoke(cm, enabled);
        //mUsbTether.setChecked(false);
//        if (cm.setUsbTethering(enabled) != TETHER_ERROR_NO_ERROR) {
//            mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
//            return;
//        }
//        mUsbTether.setSummary("");
    }




}