Skip to content

Instantly share code, notes, and snippets.

@PPartisan
Created September 19, 2016 11:02
Show Gist options
  • Save PPartisan/617016b194dfb20e3b2888a491ab5a30 to your computer and use it in GitHub Desktop.
Save PPartisan/617016b194dfb20e3b2888a491ab5a30 to your computer and use it in GitHub Desktop.
Demo for an Android app that interacts with the browser and its bookmarks (API < 22)
<?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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
tools:context="com.github.ppartisan.bookmarklet.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/text" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="@string/launch_browser"/>
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:id="@+id/list" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.ppartisan.bookmarklet">
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
</application>
</manifest>
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Browser;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, LoaderManager.LoaderCallbacks<Cursor> {
private static final String MIME_TYPE = "text/";
private static final Uri DATA = Uri.parse("http://www.quora.com");
private CursorAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTextView = (TextView) findViewById(R.id.text);
Button mButton = (Button) findViewById(R.id.button);
ListView mListView = (ListView) findViewById(R.id.list);
mAdapter = new SimpleCursorAdapter(
this,
android.R.layout.two_line_list_item,
null,
new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL },
new int[] { android.R.id.text1, android.R.id.text2 },
0
);
mListView.setAdapter(mAdapter);
mButton.setOnClickListener(this);
mTextView.setText(R.string.empty_text_message);
final String action = getIntent().getAction();
final String type = getIntent().getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if (type.startsWith(MIME_TYPE)) {
mTextView.setText(getIntent().getStringExtra(Intent.EXTRA_TEXT));
}
}
getSupportLoaderManager().initLoader(0, null, this);
}
@Override
public void onClick(View view) {
Intent launchBrowser = new Intent(Intent.ACTION_VIEW);
launchBrowser.setData(DATA);
startActivity(launchBrowser);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
final String[] projection = {
Browser.BookmarkColumns._ID,
Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL,
Browser.BookmarkColumns.BOOKMARK
};
return new CursorLoader(this, Browser.BOOKMARKS_URI, projection, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment