Code example for ComponentName

Methods: flattenToString

0
        static final String KEY_COMPONENT = "c";
        static final String KEY_LABEL = "l";
 
        public ListenerWarningDialogFragment setListenerInfo(ComponentName cn, String label) {
            Bundle args = new Bundle();
            args.putString(KEY_COMPONENT, cn.flattenToString());
            args.putString(KEY_LABEL, label);
            setArguments(args);
 
            return this;
        } 
        @Override 
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final Bundle args = getArguments();
            final String label = args.getString(KEY_LABEL);
            final ComponentName cn = ComponentName.unflattenFromString(args.getString(KEY_COMPONENT));
 
            final String title = getResources().getString(
                    R.string.notification_listener_security_warning_title, label);
            final String summary = getResources().getString(
                    R.string.notification_listener_security_warning_summary, label);
            return new AlertDialog.Builder(getActivity())
                    .setMessage(summary)
                    .setTitle(title)
                    .setIconAttribute(android.R.attr.alertDialogIcon)
                    .setCancelable(true)
                    .setPositiveButton(android.R.string.ok,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    mEnabledListeners.add(cn);
                                    saveEnabledListeners(); 
                                } 
                            }) 
                    .setNegativeButton(android.R.string.cancel,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // pass 
                                } 
                            }) 
                    .create();
        } 
    } 
 
    @Override 
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
 
        mPM = getActivity().getPackageManager();
        mCR = getActivity().getContentResolver();
        mList = new ListenerListAdapter(getActivity());
    } 
 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.notification_access_settings, container, false);
    } 
 
    @Override 
    public void onResume() { 
        super.onResume(); 
        updateList(); 
 
        // listen for package changes 
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        filter.addDataScheme("package");
        getActivity().registerReceiver(mPackageReceiver, filter);
 
        mCR.registerContentObserver(ENABLED_NOTIFICATION_LISTENERS_URI, false, mSettingsObserver);
    } 
 
    @Override 
    public void onPause() { 
        super.onPause(); 
 
        getActivity().unregisterReceiver(mPackageReceiver);
        mCR.unregisterContentObserver(mSettingsObserver);
    } 
 
    void loadEnabledListeners() { 
        mEnabledListeners.clear();
        final String flat = Settings.Secure.getString(mCR,
                Settings.Secure.ENABLED_NOTIFICATION_LISTENERS); 
        if (flat != null && !"".equals(flat)) {
            final String[] names = flat.split(":");
            for (int i = 0; i < names.length; i++) {
                final ComponentName cn = ComponentName.unflattenFromString(names[i]);
                if (cn != null) {
                    mEnabledListeners.add(cn);
                } 
            } 
        } 
    } 
 
    void saveEnabledListeners() { 
        StringBuilder sb = null;
        for (ComponentName cn : mEnabledListeners) {
            if (sb == null) {
                sb = new StringBuilder();
            } else { 
                sb.append(':');
            } 
            sb.append(cn.flattenToString());
        } 
        Settings.Secure.putString(mCR,
                Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, 
                sb != null ? sb.toString() : "");
    } 
 
    void updateList() { 
        loadEnabledListeners(); 
 
        getListeners(mList, mPM);
        mList.sort(new PackageItemInfo.DisplayNameComparator(mPM));
 
        getListView().setAdapter(mList);
    } 
 
    static int getListenersCount(PackageManager pm) {
        return getListeners(null, pm);
    } 
 
    private static int getListeners(ArrayAdapter<ServiceInfo> adapter, PackageManager pm) {
        int listeners = 0;
        if (adapter != null) {
            adapter.clear();
        } 
        final int user = ActivityManager.getCurrentUser();
 
        List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
                new Intent(NotificationListenerService.SERVICE_INTERFACE),
                PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
                user);
 
        for (int i = 0, count = installedServices.size(); i < count; i++) {
            ResolveInfo resolveInfo = installedServices.get(i);
            ServiceInfo info = resolveInfo.serviceInfo;
 
            if (!android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE.equals( 
                    info.permission)) {
                Slog.w(TAG, "Skipping notification listener service "
                        + info.packageName + "/" + info.name
                        + ": it does not require the permission " 
                        + android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE); 
                continue; 
            } 
            if (adapter != null) {
                adapter.add(info);
            } 
            listeners++;
        } 
        return listeners;
    } 
 
    boolean isListenerEnabled(ServiceInfo info) {
        final ComponentName cn = new ComponentName(info.packageName, info.name);
        return mEnabledListeners.contains(cn);
    } 
 
    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) {
        ServiceInfo info = mList.getItem(position);
        final ComponentName cn = new ComponentName(info.packageName, info.name);
        if (mEnabledListeners.contains(cn)) {
            // the simple version: disabling 
            mEnabledListeners.remove(cn);
            saveEnabledListeners(); 
        } else {