com.github.pennyfive.sqlitestudio.ui.PackagesFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.github.pennyfive.sqlitestudio.ui.PackagesFragment.java

Source

/*
 * Copyright 2014 Joonas Lehtonen
 *
 * 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.
 */

package com.github.pennyfive.sqlitestudio.ui;

import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.github.pennyfive.sqlitestudio.DatabaseScanLoader;
import com.github.pennyfive.sqlitestudio.DatabaseScanLoader.Result;
import com.github.pennyfive.sqlitestudio.DatabaseScanLoader.ScannedPackage;
import com.github.pennyfive.sqlitestudio.R;
import com.github.pennyfive.sqlitestudio.ui.adapter.BinderAdapter;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *
 */
public class PackagesFragment extends Fragment implements LoaderCallbacks<Result> {
    private static final int NUM_DATABASES_WHEN_COLLAPSED = 3;

    @InjectView(R.id.list)
    ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLoaderManager().initLoader(0, savedInstanceState, this);
    }

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

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.inject(this, view);
    }

    @Override
    public Loader<Result> onCreateLoader(int id, Bundle args) {
        return new DatabaseScanLoader(getActivity());
    }

    @Override
    public void onLoadFinished(Loader<Result> resultLoader, Result result) {
        TextView databaseCountHeader = (TextView) View.inflate(getActivity(), R.layout.header_text, null);
        databaseCountHeader.setText(getResources().getQuantityString(R.plurals.databases_header_num_databases,
                result.getTotalNumDatabases(), result.getTotalNumDatabases()));
        listView.addHeaderView(databaseCountHeader, null, false);

        listView.setAdapter(new BinderAdapter<ScannedPackage>(getActivity(), result.getScannedPackages()) {
            @Override
            protected View newView(LayoutInflater inflater, ViewGroup parent) {
                return inflater.inflate(R.layout.item_package, parent, false);
            }

            @Override
            protected void bindView(View view, final ScannedPackage item, int position) {
                Drawable applicationIcon = getApplicationIcon(item.getPackageName());
                ((ImageView) view.findViewById(R.id.icon)).setImageDrawable(applicationIcon);

                String applicationName = getApplicationName(item.getPackageName());
                ((TextView) view.findViewById(R.id.application_name)).setText(applicationName);

                ((TextView) view.findViewById(R.id.package_name)).setText(item.getPackageName());

                ((TextView) view.findViewById(R.id.num_databases)).setText(String.valueOf(item.getNumDatabases()));

                final ViewGroup databaseLayout = (ViewGroup) view.findViewById(R.id.database_layout);
                databaseLayout.removeAllViews();

                final LayoutInflater inflater = LayoutInflater.from(getActivity());

                final int databasesCount = item.getNumDatabases();
                int initialItemCount = databasesCount > NUM_DATABASES_WHEN_COLLAPSED + 1
                        ? NUM_DATABASES_WHEN_COLLAPSED
                        : databasesCount;
                for (int i = 0; i < initialItemCount; i++) {
                    databaseLayout
                            .addView(inflateDatabaseItem(inflater, databaseLayout, item.getDatabasePaths().get(i)));
                }

                if (databasesCount > initialItemCount) {
                    View showAllFooter = inflater.inflate(R.layout.item_package_show_all_extra, null);
                    databaseLayout.addView(showAllFooter);
                    showAllFooter.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            databaseLayout.removeView(v);
                            for (int i = databaseLayout.getChildCount(); i < databasesCount; i++) {
                                databaseLayout.addView(inflateDatabaseItem(inflater, databaseLayout,
                                        item.getDatabasePaths().get(i)));
                            }
                        }
                    });
                }
            }

            private View inflateDatabaseItem(LayoutInflater inflater, ViewGroup parent, final String databasePath) {
                View databaseItem = inflater.inflate(R.layout.item_package_database_extra, parent, false);
                String databaseFileName = databasePath.substring(databasePath.lastIndexOf('/') + 1,
                        databasePath.length());
                ((TextView) databaseItem.findViewById(R.id.text)).setText(databaseFileName);
                databaseItem.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onDatabaseSelected(databasePath);
                    }
                });
                return databaseItem;
            }
        });
    }

    @Override
    public void onLoaderReset(Loader<Result> resultLoader) {

    }

    private void onDatabaseSelected(String databasePath) {
        // TODO
    }

    private String getApplicationName(String packageName) {
        try {
            PackageManager pm = getActivity().getPackageManager();
            return (String) pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0));
        } catch (NameNotFoundException e) {
            return "";
        }
    }

    private Drawable getApplicationIcon(String packageName) {
        try {
            return getActivity().getPackageManager().getApplicationIcon(packageName);
        } catch (NameNotFoundException e) {
            return null;
        }
    }
}