switch to listview

This commit is contained in:
arrase 2016-11-20 01:52:23 +01:00
parent fa6151cc99
commit c232e1c92e
12 changed files with 129 additions and 332 deletions

View File

@ -31,5 +31,4 @@ dependencies {
compile 'com.android.support:support-v4:23.4.0' compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0' compile 'com.android.support:design:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
} }

View File

@ -1,38 +1,49 @@
package org.torproject.android.ui.hs; package org.torproject.android.ui.hs;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import org.torproject.android.R; import org.torproject.android.R;
import org.torproject.android.ui.hs.adapters.HSAdapter; import org.torproject.android.ui.hs.adapters.OnionListAdapter;
import org.torproject.android.ui.hs.dialogs.HSDataDialog; import org.torproject.android.ui.hs.dialogs.HSDataDialog;
import org.torproject.android.ui.hs.providers.HSContentProvider; import org.torproject.android.ui.hs.providers.HSContentProvider;
public class HiddenServicesActivity extends AppCompatActivity { public class HiddenServicesActivity extends AppCompatActivity {
private HSAdapter mHiddenServices;
private ContentResolver mCR; private ContentResolver mCR;
private HSObserver mHSObserver; private OnionListAdapter mAdapter;
private String[] mProjection = new String[]{ private String[] mProjection = new String[]{
HSContentProvider.HiddenService._ID, HSContentProvider.HiddenService._ID,
HSContentProvider.HiddenService.NAME, HSContentProvider.HiddenService.NAME,
HSContentProvider.HiddenService.DOMAIN, HSContentProvider.HiddenService.DOMAIN};
HSContentProvider.HiddenService.ONION_PORT,
HSContentProvider.HiddenService.PORT}; class HSObserver extends ContentObserver {
HSObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
mAdapter.changeCursor(mCR.query(
HSContentProvider.CONTENT_URI, mProjection, null, null, null
));
}
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hidden_services); setContentView(R.layout.layout_hs_list_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); mCR = getContentResolver();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() { fab.setOnClickListener(new View.OnClickListener() {
@ -43,35 +54,35 @@ public class HiddenServicesActivity extends AppCompatActivity {
} }
}); });
mCR = getContentResolver(); mAdapter = new OnionListAdapter(
// View adapter this,
mHiddenServices = new HSAdapter( getContentResolver().query(
mCR.query(
HSContentProvider.CONTENT_URI, mProjection, null, null, null HSContentProvider.CONTENT_URI, mProjection, null, null, null
)); ),
0
);
mHSObserver = new HSObserver(new Handler()); mCR.registerContentObserver(
mCR.registerContentObserver(HSContentProvider.CONTENT_URI, true, mHSObserver); HSContentProvider.CONTENT_URI, true, new HSObserver(new Handler())
);
// Fill view ListView onion_list = (ListView) findViewById(R.id.onion_list);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.onion_list); onion_list.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mHiddenServices);
}
class HSObserver extends ContentObserver { onion_list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public HSObserver(Handler handler) {
super(handler);
}
@Override @Override
public void onChange(boolean selfChange) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// New data
mHiddenServices.changeCursor(mCR.query(
HSContentProvider.CONTENT_URI, mProjection, null, null, null
));
}
} }
});
onion_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
return false;
}
});
}
} }

View File

@ -1,144 +0,0 @@
package org.torproject.android.ui.hs.adapters;
/*
* Copyright (C) 2014 skyfish.jy@gmail.com
*
* 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.
*
*/
import android.database.Cursor;
import android.database.DataSetObserver;
import android.support.v7.widget.RecyclerView;
/**
* Created by skyfishjy on 10/31/14.
*/
public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
private Cursor mCursor;
private boolean mDataValid;
private int mRowIdColumn;
private DataSetObserver mDataSetObserver;
public CursorRecyclerViewAdapter(Cursor cursor) {
mCursor = cursor;
mDataValid = cursor != null;
mRowIdColumn = mDataValid ? mCursor.getColumnIndex("_id") : -1;
mDataSetObserver = new NotifyingDataSetObserver();
if (mCursor != null) {
mCursor.registerDataSetObserver(mDataSetObserver);
}
}
@Override
public int getItemCount() {
if (mDataValid && mCursor != null) {
return mCursor.getCount();
}
return 0;
}
@Override
public long getItemId(int position) {
if (mDataValid && mCursor != null && mCursor.moveToPosition(position)) {
return mCursor.getLong(mRowIdColumn);
}
return 0;
}
@Override
public void setHasStableIds(boolean hasStableIds) {
super.setHasStableIds(true);
}
public abstract void onBindViewHolder(VH viewHolder, Cursor cursor);
@Override
public void onBindViewHolder(VH viewHolder, int position) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
onBindViewHolder(viewHolder, mCursor);
}
/**
* Change the underlying cursor to a new cursor. If there is an existing cursor it will be
* closed.
*/
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
public void closeCursor() {
mCursor.unregisterDataSetObserver(mDataSetObserver);
mCursor.close();
}
/**
* Swap in a new Cursor, returning the old Cursor. Unlike
* {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
* closed.
*/
private Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
final Cursor oldCursor = mCursor;
if (oldCursor != null && mDataSetObserver != null) {
oldCursor.unregisterDataSetObserver(mDataSetObserver);
}
mCursor = newCursor;
if (mCursor != null) {
if (mDataSetObserver != null) {
mCursor.registerDataSetObserver(mDataSetObserver);
}
mRowIdColumn = newCursor.getColumnIndexOrThrow("_id");
mDataValid = true;
notifyDataSetChanged();
} else {
mRowIdColumn = -1;
mDataValid = false;
notifyDataSetChanged();
//There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
}
return oldCursor;
}
private class NotifyingDataSetObserver extends DataSetObserver {
@Override
public void onChanged() {
super.onChanged();
mDataValid = true;
notifyDataSetChanged();
}
@Override
public void onInvalidated() {
super.onInvalidated();
mDataValid = false;
notifyDataSetChanged();
//There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
}
}
}

View File

@ -1,55 +0,0 @@
package org.torproject.android.ui.hs.adapters;
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.torproject.android.R;
import org.torproject.android.ui.hs.providers.HSContentProvider;
public class HSAdapter extends CursorRecyclerViewAdapter<HSAdapter.ViewHolder> {
public HSAdapter(Cursor cursor) {
super(cursor);
}
@Override
public HSAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.onion_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {
viewHolder.id = cursor.getInt(cursor.getColumnIndex(HSContentProvider.HiddenService._ID));
String name_string = cursor.getString(cursor.getColumnIndex(HSContentProvider.HiddenService.NAME));
Integer port = cursor.getInt(cursor.getColumnIndex(HSContentProvider.HiddenService.PORT));
Integer onion_port = cursor.getInt(cursor.getColumnIndex(HSContentProvider.HiddenService.ONION_PORT));
viewHolder.name.setText(name_string + ": " + port.toString()+ " -> " +onion_port.toString());
viewHolder.domain.setText(
cursor.getString(cursor.getColumnIndex(HSContentProvider.HiddenService.DOMAIN))
);
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView domain;
Integer id;
ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.hs_name);
domain = (TextView) itemView.findViewById(R.id.hs_onion);
}
}
}

View File

@ -0,0 +1,36 @@
package org.torproject.android.ui.hs.adapters;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.torproject.android.R;
import org.torproject.android.ui.hs.providers.HSContentProvider;
public class OnionListAdapter extends CursorAdapter {
private LayoutInflater cursorInflater;
public OnionListAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
cursorInflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name = (TextView) view.findViewById(R.id.hs_name);
name.setText(cursor.getString(cursor.getColumnIndex(HSContentProvider.HiddenService.NAME)));
TextView domain = (TextView) view.findViewById(R.id.hs_onion);
domain.setText(cursor.getString(cursor.getColumnIndex(HSContentProvider.HiddenService.DOMAIN)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return cursorInflater.inflate(R.layout.layout_hs_list_item, parent, false);
}
}

View File

@ -5,14 +5,11 @@ import android.app.Dialog;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.ContentValues; import android.content.ContentValues;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.BoolRes;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.app.ShareCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
@ -25,7 +22,7 @@ public class HSDataDialog extends DialogFragment {
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the layout // Get the layout
final View dialog_view = getActivity().getLayoutInflater().inflate(R.layout.dialog_hs_data, null); final View dialog_view = getActivity().getLayoutInflater().inflate(R.layout.layout_hs_data_dialog, null);
// Use the Builder class for convenient dialog construction // Use the Builder class for convenient dialog construction
final AlertDialog serverDataDialog = new AlertDialog.Builder(getActivity()) final AlertDialog serverDataDialog = new AlertDialog.Builder(getActivity())

View File

@ -1,34 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="org.torproject.android.ui.hs.HiddenServicesActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/DefaultTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/DefaultTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_hidden_services" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/stat_notify_more" />
</android.support.design.widget.CoordinatorLayout>

View File

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_hidden_services"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="org.torproject.android.ui.hs.HiddenServicesActivity"
tools:showIn="@layout/activity_hidden_services">
<android.support.v7.widget.RecyclerView
android:id="@+id/onion_list"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/hs_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp" />
<TextView
android:id="@+id/hs_onion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>

View File

@ -0,0 +1,22 @@
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/onion_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@android:drawable/stat_notify_more"
app:layout_anchor="@id/onion_list"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>

View File

@ -1,33 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/hs_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:paddingLeft="5dp" />
<TextView
android:id="@+id/hs_onion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:paddingLeft="5dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>